Chapter 6 Files and Exception Flashcards
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’
‘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.
A(n) ___ access file is also known as direct access file.
a. numbered
b. text
c. sequential
d. random
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.
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
open the file
Chapter 6 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
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.
Python allows the programmer to work with text and number files.
True or False?
False
Chapter 6 Q (page# 289)
Python allows you to work both text files and ‘binary’ files.
If the last line in the file is not terminated with \n, the readline method will return the line without \n.
True or False?
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.
Which method could be used to strip specific characters from the end of a string?
a. remove
b. estrip
c. strip
d. rstrip
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.
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’)
customer.write(‘Mary Smith’)
Chapter 6 Q
Closing a file disconnects the communication between the file and the program.
True or False?
True
Chapter 6 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?
False
Chapter 6 Q