Chapter 6 - Filenames and exceptions Flashcards
Define Persistence
Programs that run for a long time, or all the time.
Some data is in the permanent storage(hard drive)
A shutdown/restart sequence has them picking up where the program ended.
Examples: operating systems, web servers, apps on phone
Define Transient
These programs run for a short time and produce some output but when they end the data disappears. If you run the program again it starts with a clean slate.
Output File
A file that data is written to it
Input file
a file from which data is read
What are the 3 steps when a program uses a file?
Open file, process file, close file
What are the two general types of files?
Text file
Binary File
What is a text file?
contains data that has been encoded as text
What is a binary file?
Contains data that has not been converted to text. (faster for computer because it is already in binary)
What are the two ways to access data stored in a file:
sequential access, direct access
Define Sequential access
File read sequentially from beginning to end, can’t skip ahead (cassette tape)
Define Direct access
can jump directly to any piece of data in a file ( a CD)
What are filename extensions?
short sequences of characters that appear at the end of a filename preceded by a period.
Extension indicates type of data stored in the file.
What is a file object?
object associated with a specific file
provides a way for a program to work with the file. file object referenced BY A VARIABLE.
File objects allow us to use,access, and. manipulate all the user accessible files.
What does the open function do?
used to open a file.
creates a file object and associates it with a file on the disc.
Format:
file_object = open(‘filename’,’mode’)
What is a mode? what are the three types that you can use when opening a file?
A mode is a string specifiying how the file will be opened.
‘r’ = read
‘w’ = write
‘a’ = append
What is a method?
A function that belongs to an object.
performs operations using that object.
example: file objects write method used to write data to a file
format: file_variable.write(string)
What is the read method?
file object method that reads entire file contents into memory.
Only works if the file was opened for reading.
The contents are returned as a string
What is the readline method?
file object method that reads a line from the file
Line returned as a string, including ‘\n’
Define read position:
marks the location of the next item to be read from a file
What is the rstrip method?
string method that strips specific characters from the end of the string
What happens if you open the file in ‘w’ mode if the file already exists
the file will be overwritten
How do you write numbers to a file
You must first convert them to a string
What does the str function do:
converts a value to a string
numbers are read from a text file as strings.
Must be converted to numeric type in order to perform mathematical calculations.
Use int and float functions to convert strings to numeric values.
Which method uses an empty string as a sentinel when end of file is reached?
readline method.
while line != ‘ ‘ #does not equal empty