Starting Out with Python, Ch 6

Ace your homework & exams now with Quizwiz!

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.

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

False

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

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

IOError

What is a field?

an individual piece of data within a record

When an exception is generated, it is said to have been _______ a. built b. raised c. caught d. killed

raised

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

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

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

What are the two types of file access?

sequential access and direct access

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

False

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

False

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

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

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.

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.

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

Open the file Process the file Close the file

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

The file will be created.

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.

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

True

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

ValueError

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.

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.

What is a record?

a complete set of data about an item

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.

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

Briefly describe what an exception is.

an error that occurs while a program is running

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.

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

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

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

What is a traceback?

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

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

This is a single piece of data within a record. a. field b. variable c. delimiter d. subrecord

field

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.

Before a file can be used by a program, it must be a. formatted. b. encrypted. c. closed. d. opened.

opened

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

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

You write this statement to respond to exceptions. a. run/ handle b. try/except c. try /handle d. attempt/except

try/except

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.

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.

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.

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.

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

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.

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

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

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

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.

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

'r' mode

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.

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.

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

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

True

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

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.


Related study sets

Visual Arts Content Test Subset 1

View Set

Slope, Parallel and Perpendicular Lines, Graphing Lines, Inverse Linear Functions

View Set

1. Basic surgical approaches to intracranial space

View Set