1/32
Vocabulary terms covering file I/O operations, file modes, loops for file processing, the with statement, record processing, and exception handling in Python.
Output file
A file that data is written to.
Input file
A file from which data is read.
Three steps when a program uses a file
Text file
A file containing data that has been encoded as text.
Binary file
A file containing data that has not been converted to text.
Sequential access
A file access method where a file is read sequentially from beginning to end and cannot skip ahead.
Direct access
A file access method that allows a program to jump directly to any piece of data in the file.
Filename extension
A short sequence of characters that appears at the end of a filename preceded by a period, indicating the type of data stored in the file.
File object
An object associated with a specific file that provides a way for a program to work with the file and is referenced by a variable.
open function
A function used to open a file by creating a file object and associating it with a file on the disk using the general format file_object = open(filename, mode).
File mode
A string argument passed to the open function that specifies how the file will be opened, such as reading only ('r'), writing ('w'), and appending ('a').
Raw string literal (r prefix)
A string literal prefixed with the letter r, used when specifying an alternative path and file name argument in the open function.
Method
A function that belongs to an object and performs operations using that object.
write method
A file object method used to write data to a file in the format file_variable.write(string).
close method
A file object method used to close an open file in the format file_variable.close().
read method
A file object method that reads the entire file contents into memory as a string, working only when the file is opened for reading.
readline method
A file object method that reads a line from the file and returns it as a string, including the newline character ('\n').
Read position
A marker that marks the location of the next item to be read from a file.
rstrip method
A string method that strips specific characters from the end of a string, commonly used to remove '\n' from a string after reading it from a file.
Append mode ('a')
A file opening mode where if the file exists it is not erased, if it does not exist it is created, and data is written to the end of the current contents.
Empty string ('')
A sentinel value returned by the readline method when the end of a file has been reached.
with statement
A statement used to open a file and automatically close it once the enclosed suite finishes executing.
with suite
The indented block of statements following a with statement.
Record
A complete set of data that describes one item.
Field
A single piece of data within a record.
Exception
An error that occurs while a program is running, which usually causes the program to abruptly halt.
Traceback
An error message that gives information regarding the line numbers that caused an exception, the exception type, and a brief description of the error.
Exception handler
Code written as a try/except statement that responds when exceptions are raised and prevents the program from crashing.
Try suite
The block of statements contained in a try block that can potentially raise an exception.
Handler
The statements contained inside an except block that execute if an exception is raised in the try suite.
Exception object
An object created in memory when an exception is thrown, usually containing the default error message pertaining to that exception.
else clause (in try/except)
An optional clause aligned with try and except that executes its block of statements only if no exceptions were raised in the try suite.
finally clause
An optional clause in a try/except statement whose suite executes whether an exception occurs or not, primarily to perform cleanup before exiting.