Topic 3 - Persistence & Inheritance Flashcards
File Input and Output
The process of opening a file, processing it by writing or reading data, and closing the file
Persistence
The ability of a program to save data for later use
Binary files
Data not encoded as text, not human readable
Text files
Data encoded as text as a series of characters, human readable
Sequential Access
Accessing data from the beginning of the file to the end of the file
Direct Access
Jumping directly to any piece of data in the file without reading the data that comes before it
File object
An object associated with a specific file that provides a way for the program to work with the file
Open() function
Used to open a file, given its file name and the operation mode
‘r’ mode
Read mode, file cannot be changed
‘w’ mode
Write mode, new file will be created
‘a’ mode
Append mode, data will be appended to the end of the file
write() method
Used to write a string to the file
close() method
Used to close the file object
read() method
Used to read the entire content from the file
readline() method
Used to read line by line from the file
Detecting End of File
Calling readline() before entering a while loop to test for empty string, or using a for loop
Shelve
Python object persistence, a dictionary-like object for persistent storage
shelve.open()
Used to open a persistent storage for reading and writing, creating it if it doesn’t exist
db[‘key’]
Used to store and retrieve data in the underlying database
db.close()
Used to close the underlying database when it is not in use
Exception
An error that occurs while a program is running
try/except statement
Used to handle exceptions gracefully
else clause
Executed if no exceptions are raised
finally clause
Ensures that certain code is always executed, regardless of whether an exception is raised or not