Why must programs use files?
To retain data between program runs
What is writing data to a file?
Saving data on a file (output file)
What is reading data from a file?
Retrieving data from a file (input file)
What are the three steps when a program uses a file?
Open the file, process the file, close the file
What are the two main types of files?
Text files and binary files
What are the two ways to access file data?
Sequential access and direct access
What is a filename extension?
A short sequence of characters at the end of a filename indicating file type
What does Python’s open() function do?
Opens a file, creates a file object, and associates it with a disk file
What are the common file modes in Python?
‘r’ for reading, ‘w’ for writing, ‘a’ for appending
What happens when you open a file with ‘w’ mode?
If it exists, it is overwritten; if not, it is created
What happens when you open a file with ‘a’ mode?
Data is written to the end of the file without erasing existing content
What does write() do?
Writes a string to a file
What does read() do?
Reads the entire file into a string
What does readline() do?
Reads one line from a file, including the \n
What does rstrip() do when reading files?
Strips trailing characters (like \n) from a string
Why must numbers be converted before writing to a file?
They must be converted to strings using str()
Why must numbers be converted after reading from a file?
They are read as strings and must be converted with int() or float()
How does Python detect the end of a file using readline()?
An empty string ‘’ is returned
How do you use a for loop to read files?
for line in file_object:
statements
What does the with statement do when opening files?
Opens and automatically closes files when the block ends
What is a record?
A set of data that describes one item
What is a field?
A single piece of data within a record
What is an exception in Python?
An error that occurs during program execution
What is a traceback?
An error message showing the type of exception and line number