Chapter 6 Files and Exception Flashcards

1
Q

Which mode specifier will open a file but not let you change the file or write to it?

a. ‘r’
b. ‘a’
c. ‘e’
d. ‘w’

A

‘r’

Chapter 6 Q (page# 291)

‘r’ Open a file for reading only. The file cannot be changed or written to.

‘w’ Open a file for writing. If the file already exists, erase its contents. If it does not exist, create it.

‘a’ Open a file to be written to. All data written to the file will be appended to its end. if the file does not exist, create it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

A(n) ___ access file is also known as direct access file.

a. numbered
b. text
c. sequential
d. random

A

random

Chapter 6 Q (page# 290)

When you work with a direct access file (also known as random access file), you can jump directly to any piece of data in the file without reading the data that comes before it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Which step creates a connection between a file and a program?

a. open the file
b. read the file
c. process the file
d. close the file

A

open the file

Chapter 6 Q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which of the following is associated with a specific file and provides a way for the program to work with that file?

a. the filename
b. the file variable
c. the file extension
d. the file object

A

the file object

Chapter 6 Q (page 290)

In order for a program to work with a file on the computer’s disk, the program must create a file object in memory.
In the program, a variable references the file object. This variable is used to carry out any operations that are performed on the file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Python allows the programmer to work with text and number files.
True or False?

A

False

Chapter 6 Q (page# 289)

Python allows you to work both text files and ‘binary’ files.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

If the last line in the file is not terminated with \n, the readline method will return the line without \n.
True or False?

A

True

Chapter 6 Q (page 295)

In Python, you use the readline method to read a line from a file. ( A line is imply a string of characters that are terminated with a \n). The method returns the line as a string, including the \n.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which method could be used to strip specific characters from the end of a string?

a. remove
b. estrip
c. strip
d. rstrip

A

rstrip

Chapter 6 Q (page# 299)

Python has a method named, rstrip that removes or “strips” specific characters from the end of the string.
It is named rstrip because it strips characters from the right side of a string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Given that the customer file references a file object and the file was opened using the ‘w’ mode specifier, how would you write the string ‘Mary Smith’ to the file?

a. customer file.write(‘MarySmith’)
b. customer.input(‘Mary Smith’)
c. customer.write(‘Mary Smith’)
d. customer.write(‘w’, ‘Mary Smith’)

A

customer.write(‘Mary Smith’)

Chapter 6 Q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Closing a file disconnects the communication between the file and the program.
True or False?

A

True

Chapter 6 Q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

In Python, there is nothing that can be done if the program tries to access a file to read that does not exist.
True or False?

A

False

Chapter 6 Q

How well did you know this?
1
Not at all
2
3
4
5
Perfectly