Final Exam Prep Flashcards
Which mode specifier will open a file but will not let you change the file or write to it?
read
Which mode specifier will erase the contents of a file if it already exists and create it if it does not exist?
write
Assume that there is a variable named customer that references a file object. How would you write the string ‘Mary Smith’ to the file?
customer.write(‘Mary Smith’)
If a file has been opened properly, which method will return the file’s entire contents as a string?
read()
Which method could be used to strip specific characters from just the end of a string?
rstrip()
Given that the file is valid and contains numbers, what does x contain after the following statement?
x = open(“numbers.txt”, “r”)
a file object
Which of these is not a valid file mode when opening a file in Python?
f
Before a file can be used by a program, it must be _________.
opened
This is a single piece of data within a record.
field
When a file is opened in this mode, data will be written at the end of the file’s existing contents.
append mode
To respond to errors, what kind of a statement must be used?
try/except
Which of these causes an IOError?
Opening a file that doesn’t exist.
Which of these is associated with a specific file, and provides a way for the program to work with that file?
File object
What do you call the process of retrieving data from a file?
Reading
A(n)__________ is a function that belongs to an object, and performs some operation using that object.
method
A(n) ______ is a complete set of data about an item.
record
The ________________ is one or more statements grouped together with the anticipation that they could potentially raise an exception.
try block
When opening a file in write mode, if a file with the specified name already exists, a warning message is generated. T/F
False
Closing a file disconnects the communication between the file and the program. T/F
True
In Python, there is no way to prevent a program from crashing if it attempts to read a file that does not exist. T/F
False
Strings can be written directly to a file with the write method, but numbers must always be converted to strings before they can be written to a file. T/F
True
The while loop automatically reads a line in a file without testing for any special condition that signals the end of the file. T/F
False
A file can safely be opened and closed multiple times within the same program. T/F
True
Given a valid file that contains multiple lines of data, what is the output of the following code:
f=open(“names.txt”,”w”)
print(f)
None of these
The contents of the entire file are printed
The letter “f” is printed.
Only the first line of the file is printed
Given a valid file containing multiple lines of data, what does the print() statement print out the first time that it is called:
f=open(“data.txt”,”r”)
for line in f:
line=f.readline()
print(line)
The second line of the file
The following code could potentially raise an IOError. T/F
f=open(“names.txt”, ‘r’)
for x in f:
print(5/x.rstrip())
False
The following code could potentially raise a ZeroDivisionError. T/F
f=open(“names.txt”, ‘r’)
for x in f:
print(5/x.rstrip())
True
When a file is first open successfully, where is the read position indicator?
At the beginning of the file
What is needed to delete a single line from a file?
The creation of a new file
This is a small “holding section” in memory that many systems write data to before writing the data to a file.
Buffer