Chapter 6 Flashcards
What does “writing data” refer to?
The process of saving data in a file
-When data is written to a file, it is copied from a variable in RAM to the file
What is input file?
-Used to describe a file from which data is read. It is called an input file because the program gets input from the file.
What are the three steps that must be taken when a file is used by a program?
- Open the file. Opening a file creates a connection between the file and the program. Opening an output file usually creates the file on the disk and allows the program to write data to it. Opening an input file allows the program to read data from the file.
- Process the file. In this step, data is either written to the file (if it is an output file) or read from the file (if it is an input file).
- Close the file. When the program is finished using the file, the file must be closed. Closing a file disconnects the file from the program.
What is a text file?
-Contains data that has been encoded as text, using a scheme such as ASCII or Unicode
What is a binary file?
Contains data that has not been converted to text
-You cannot view the contents of a binary file because it is intended only for a program to read
What are the two different ways to access data stored on file?
Sequential access
Direct access
What is sequential access?
-access data from the beginning of the file to the end of the file. If you want to read a piece of data that is stored at the very end of the file, you have to read all of the data that comes before it—you cannot jump directly to the desired data.
What is direct access?
jump directly to any piece of data in the file without reading the data that comes before it.
What is a file object?
an object that is associated with a specific file and provides a way for the program to work with that file. In the program, a variable references the file object. This variable is used to carry out any operations that are performed on the file.
How do you open a file in Python?
“open” function
- The open function creates a file object and associates it with a file on the disk:
file_variable = open(filename, mode)
-file-variable is the variable that references the file
-filename is the name of the file
-mode references the mode the file will be opened with. There are three modes:
-‘r’ is for reading only
-‘w’ is for writing (erases contents)
EXAMPLE: customer_file = open(‘customers.txt’, ‘r’)
-‘a’ opens a file to be written to
How would you open a Python file in a different location?
test_file = open(r’C:\Users\Blake\temp\test.txt’, ‘w’)
-use the r’ prefix
What is a method?
-A function that belongs to an object and performs some operation using that object:
name = ‘Charles Pace’
customer_file.write(name)
-Writes the variable ‘name’ to the file
Why should you close a file in python and how do you do that?
customer_file.close()
-Important to save data and prevent loss of data
What is the program to read a file?
# Open a file named philosophers.txt. 5 infile = open('philosophers.txt', 'r') 6 7 # Read the file's contents. 8 file_contents = infile.read() 9 10 # Close the file. 11 infile.close() 12 13 # Print the data that was read into 14 # memory. 15 print(file_contents)
What does readline do in Python?
Reads a specific line from a file: EX: line1 = infile.readline() 9 line2 = infile.readline() 10 line3 = infile.readline() 11 12 # Close the file. 13 infile.close() 14 15 # Print the data that was read into 16 # memory. 17 print(line1) 18 print(line2) 19 print(line3)