Chapter Questions
What is an output file?
What is an input file?
What three steps must be taken by a program when it uses a file?
In general, what are the two types of files? What is the difference between these two types of files?
What are the two types of file access? What is the difference between these two?
When writing a program that performs an operation on a file, what two file-associated names do you have to work with in your code?
If a file already exists, what happens to it if you try to open it as an output file (using the 'w' mode)?
What is the purpose of opening a file?
What is the purpose of closing a file?
What is a file’s read position? Initially, where is the read position when an input file is opened?
In what mode do you open a file if you want to write data to it, but you do not want to erase the file’s existing contents? When you write data to such a file, to what part of the file is the data written?
Write a short program that uses a for loop to write the numbers 1 through 10 to a file.
What does it mean when the readline method returns an empty string?
Assume the file data.txt exists and contains several lines of text. Write a short program using the while loop that displays each line in the file.
Revise the program that you wrote for Checkpoint 6.14 to use the for loop instead of the while loop.
What is a record? What is a field?
Describe the way that you use a temporary file in a program that modifies a record in a sequential access file.
Describe the way that you use a temporary file in a program that deletes a record from a sequential file.
Briefly describe what an exception is.
If an exception is raised and the program does not handle it with a try/except statement, what happens?
What type of exception does a program raise when it tries to open a nonexistent file?
What type of exception does a program raise when it uses the float function to convert a non-numeric string to a number?