Chapter 6 Flashcards

1
Q

What does “writing data” refer to?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is input file?

A

-Used to describe a file from which data is read. It is called an input file because the program gets input from the file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the three steps that must be taken when a file is used by a program?

A
  1. 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.
  2. 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).
  3. 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a text file?

A

-Contains data that has been encoded as text, using a scheme such as ASCII or Unicode

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a binary file?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the two different ways to access data stored on file?

A

Sequential access

Direct access

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is sequential access?

A

-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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is direct access?

A

jump directly to any piece of data in the file without reading the data that comes before it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a file object?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you open a file in Python?

A

“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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How would you open a Python file in a different location?

A

test_file = open(r’C:\Users\Blake\temp\test.txt’, ‘w’)

-use the r’ prefix

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a method?

A

-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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Why should you close a file in python and how do you do that?

A

customer_file.close()

-Important to save data and prevent loss of data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the program to read a file?

A
# 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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does readline do in Python?

A
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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Why must you use /n when writing data to files?

A
-ensures that each piece of data is written to a separate line in the file
  7      name1 = input('Friend #1: ')
  8      name2 = input('Friend #2: ')
  9      name3 = input('Friend #3: ')
 10
 11      # Open a file named friends.txt.
 12      myfile = open('friends.txt', 'w')
 13
 14      # Write the names to the file.
 15      myfile.write(name1 + '\n')
 16      myfile.write(name2 + '\n')
 17      myfile.write(name3 + '\n')
17
Q

What does ‘a’ mode do ?

A

It appends data to the end of existing data:

  • If the file already exists, it will not be erased. If the file does not exist, it will be created.
  • When data is written to the file, it will be written at the end of the file’s current contents.
EX: 
 myfile = open('friends.txt', 'a')
 myfile.write('Matt\n')
 myfile.write('Chris\n')
 myfile.write('Suze\n')
 myfile.close()