Starting Out with Python, Ch 6

0.0(0)
Studied by 0 people
call kaiCall Kai
Locked
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/54

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 5:36 AM on 7/23/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

55 Terms

1
New cards

A file that data is written to is known as a(n)

a. input file.

b. output file.

c. sequential access file.

d. binary file.

output file

2
New cards

A file that data is read from is known as a(n)

a. input file.

b. output file.

c. sequential access file.

d. binary file.

input file

3
New cards

Before a file can be used by a program, it must be

a. formatted.

b. encrypted.

c. closed.

d. opened.

opened

4
New cards

When a program is finished using a file, it should do this.

a. erase the file

b. open the file

c. close the file

d. encrypt the file

close the file

5
New cards

The contents of this type of file can be viewed in an editor such as Notepad.

a. text file

b. binary file

c. English file

d. human-readable file

text file

6
New cards

This type of file contains data that has not been converted to text.

a. text file

b. binary file

c. Unicode file

d. symbolic file

binary file

7
New cards

When working with this type of file, you access its data from the beginning of the file

to the end of the file.

a. ordered access

b. binary access

c. direct access

d. sequential access

sequential access

8
New cards

When working with this type of file, you can jump directly to any piece of data in the file without reading the data that comes before it.

a. ordered access

b. binary access

c. direct access

d. sequential access

direct access

9
New cards

This is a small "holding section " in memory that many systems write data to before writing the data to a file.

a. buffer

b. variable

c. virtual file

d. temporary file

buffer

10
New cards

This marks the location of the next item that will be read from a file.

a. input position

b. delimiter

c. pointer

d. read position

read position

11
New cards

When a file is opened in this mode, data will be written at the end of the file's existing contents.

a. output mode

b. append mode

c. backup mode

d. read-only mode

append mode

12
New cards

This is a single piece of data within a record.

a. field

b. variable

c. delimiter

d. subrecord

field

13
New cards

When an exception is generated, it is said to have been _______

a. built

b. raised

c. caught

d. killed

raised

14
New cards

This is a section of code that gracefully responds to exceptions.

a. exception generator

b. exception manipulator

c. exception handler

d. exception monitor

exception handler

15
New cards

You write this statement to respond to exceptions.

a. run/ handle

b. try/except

c. try /handle

d. attempt/except

try/except

16
New cards

When working with a sequential access file, you can jump directly to any piece of data in the file without reading the data that comes before it.

T or F

False

17
New cards

When you open a file that file already exists on the disk using the "w" mode, the contents of the existing file will be erased.

T or F

True

18
New cards

The process of opening a file is only necessary with input files. Output files are automatically opened when data is written to them.

T or F

False

19
New cards

When an input file is opened, its read position is initially set to the first item in the file.

T or F

True

20
New cards

When a file that already exists is opened in append mode, the file's existing contents are erased.

T or F

False

21
New cards

If you do not handle an exception, it is ignored by the Python interpreter, and the program continues to execute.

T or F

False

22
New cards

You can have more than one except clause in a try/except statement.

T or F

True

23
New cards

The else suite in a try/except statement executes only if a statement in the try suite raises an exception.

T or F

False

24
New cards

The finally suite in a try/except statement executes only if no exceptions are raised by statements in the try suite.

T or F

False

25
New cards

Describe the three steps that must be taken when a file is used by a program.

Open 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 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 file - When the program is finished using the file, the file must be closed.

Closing a file disconnects the file from the program.

26
New cards

Why should a program close a file when it's finished using it?

Closing a file disconnects the program from the file. In some systems, failure to close an output file can cause a loss of data.

27
New cards

What is a file's read position? Where is the read position when a file is first opened for reading?

A special value known as a read position that is internally maintained for that file. A file's read position marks the location of the next item that will be read from the file.

Initially, the read position is set to the beginning of the file.

28
New cards

If an existing file is opened in append mode, what happens to the file's existing contents?

It will not be erased and new data will be written at the end of the file's current contents.

29
New cards

If a file does not exist and a program attempts to open it in append mode, what happens?

The file will be created.

30
New cards

What is an output file?

a file that data is written to. It is called an output file because the program stores output in it.

31
New cards

What is an input file?

a file that data is read from. It is called an input file because the program gets input from the file.

32
New cards

What three steps must be taken by a program when it uses a file?

Open the file

Process the file

Close the file

33
New cards

In general, what are the two types of files? What is the difference between these two types of files?

text and binary

A text file contains data that has been encoded as text, using a scheme such as ASCII or Unicode. The file may be opened and viewed in a text editor such as Notepad.

A binary file contains data that has not been converted to text. The data that is stored in a binary file is intended only for a program to read. You cannot view the contents of a binary file with a text editor.

34
New cards

What are the two types of file access?

sequential access and direct access

35
New cards

What is the difference between sequential access and direct access files?

a sequential access file, you 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.

a direct access file (which is also known as a random access file), you can jump directly to any piece of data in the file without reading the data that comes before it

36
New cards

When writing a program that performs an operation on a file, what two file associated names do you have to work with in your code?

file_variable and filename

file_variable is the name of the variable that will reference the file object.

filename is a string specifying the name of the file.

37
New cards

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.

38
New cards

What is the purpose of opening a file?

You use the open function in Python to open a file. The open function creates a file object and associates it with a file on the disk.

39
New cards

What is the purpose of closing a file?

Closing a file disconnects the program from the file. The process of closing an output file forces any unsaved data that remains in the buffer to be written to the file. In some systems, failure to close an output file can cause a loss of data.

40
New cards

What is a file's read position? Initially, where is the read position when an input file is opened?

it marks the location of the next item that will be read from the file. When a file is opened for reading, a special value known as a read position is internally maintained for that file.

Initially, the read position is set to the beginning of the file.

41
New cards

If a file already exists what happens to it if you try to open it as an output file (using the ' w ' mode )?

If a file with the specified name already exists when the file is opened, the contents of the existing file will be erased.

42
New cards

In what mode do you open a file if you want to write data to it, but you do not want to erase the file's existing contents?

'a' mode

43
New cards

In what mode do you open a file that you do not want the file to be changed or written to?

'r' mode

44
New cards

When you write data to a file opened in 'a' mode, to what part of the file is the data written?

All data written to the file will be appended to its end.

45
New cards

What does it mean when the readline method returns an empty string?

it has attempted to read beyond the end of a file.

46
New cards

What is a priming read? What is it's purpose?

The initial read operation. The purpose is to get the first line in the file, so it can be tested by the loop.

47
New cards

What is a record?

a complete set of data about an item

48
New cards

What is a field?

an individual piece of data within a record

49
New cards

Describe the way that you use a temporary file in a program that modifies a record in a sequential access file.

You copy all of the original file's records to the temporary file, but when you get to the record that is to be modified, you do not write its old contents to the temporary file. Instead, you write its new modified values to the temporary file. Then, you finish copying any remaining records from the original file to the temporary file.

The temporary file then takes the place of the original file. You delete the original file and rename the temporary file, giving it the name that the original file had on the computer's disk.

50
New cards

Describe the way that you use a temporary file in a program that deletes a record from a sequential file.

You copy all of the original file's records to the temporary file, except for the record that is to be deleted. The temporary file then takes the place of the original file. You delete the original file and rename the temporary file, giving it the name that the original file had on the computer's disk.

51
New cards

Briefly describe what an exception is.

an error that occurs while a program is running

52
New cards

If an exception is raised and the program does not handle it with a try/except statement, what happens?

In most cases, an exception

causes a program to abruptly halt.

53
New cards

What type of exception does a program raise when it tries to open a nonexistent file?

IOError

54
New cards

What type of exception does a program raise when it uses the float function to convert a non-numeric string to a number?

ValueError

55
New cards

What is a traceback?

error message that gives information regarding the line number(s) that caused the exception