Ch6 Python
Ch6 Python
Ch6 Python
JAAMACADDA SIMAD
Faculty:
Program:
Class:
ID No. :
Phone No. :
Subject : Pearson Starting Out with Python 5rd Tony Gaddis (2021)
A2) A file from which a program reads data. It is called an input file because the program receives input
from it.
Q3) What three steps must be taken by a program when it uses a file?
A3) (1) Open the file. (2) Process the file. (3) Close the file.
Q4) In general, what are the two types of files? What is the difference between these two types of
files?
A4) Text and binary. A text file contains data that has been encoded as text using a scheme such as
ASCII. Even if the file contains numbers, those numbers are stored in the file as a series of characters.
Q5) What are the two types of file access? What is the difference between these two?
A5) Sequential and direct access. When you work with a sequential access file, you access data from the
beginning of the file to the end of the file. When you work with a direct access file, you can jump directly
to any piece of data in the file without reading the data that comes before it.
Q6) 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?
A6) The file’s name on the disk and the name of a variable that references a file object.
Q7) If a file already exists, what happens to it if you try to open it as an output file (using the 'w'
mode)?
A8) Opening a file creates a connection between the file and the program. It also creates an association
between the file and a file object.
A10) A file’s read position marks the location of the next item that will be read from the file. When an
input file is opened, its read position is initially set to the first item in the file.
Q11) 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? When you write data to such a file, to what part of the file is the data written?
Q12) Write a short program that uses a for loop to write the numbers 1 through 10 to a file.
outfile.write(str(num) + '\n')
outfile.close()
Q13) What does it mean when the readline method returns an empty string?
A13) The readline method returns an empty string ('') when it has attempted to read beyond the end of
a file.
Q14) Assume the file data.txt exists and contains several lines of text. Write a short program using the
while loop that displays each line in the file.
line = infile.readline()
print(line)
line = infile.readline()
infile.close()
Q15) Revise the program that you wrote for Checkpoint 6.14 to use the for loop instead of the while
loop.
print(line)
infile.close()
A16) A record is a complete set of data that describes one item, and a field is a single piece of data
within a record.
Q17) Describe the way that you use a temporary file in a program that modifies a record in a
sequential access file.
Q18) Describe the way that you use a temporary file in a program that deletes a record from a
sequential file.
A18) You copy all 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.
A19) An exception is an error that occurs while a program is running. In most cases, an exception causes
a program to abruptly halt.
Q20) If an exception is raised and the program does not handle it with a try/except statement, what
happens?
Q21) What type of exception does a program raise when it tries to open a nonexistent file?
A21) IOError
Q22) What type of exception does a program raise when it uses the float function to convert a non-
numeric string to a number?
A22) ValueError
a. erase the file b. open the file c. close the file d. encrypt the file
5. The contents of this type of file can be viewed in an editor such as Notepad.
6. This type of file contains data that has not been converted to text.
7. When working with this type of file, you access its data from the beginning of the file to the end of
the file.
8. 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.
9. This is a small “holding section” in memory that many systems write data to before writing the data
to a file.
10. This marks the location of the next item that will be read from a file.
11. When a file is opened in this mode, data will be written at the end of the file’s existing contents.
True or False
False 1. 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.
True 2. When you open a file that file already exists on the disk using the 'w' mode, the con- tents of
the existing file will be erased.
False 3. The process of opening a file is only necessary with input files. Output files are automatically
opened when data is written to them.
True 4. When an input file is opened, its read position is initially set to the first item in the file.
False 5. When a file that already exists is opened in append mode, the file’s existing contents are
erased.
False 6. If you do not handle an exception, it is ignored by the Python interpreter, and the program
continues to execute.
True 7. You can have more than one except clause in a try/except statement.
False 8. The else suite in a try/except statement executes only if a statement in the try suite raises an
exception.
False 9. The finally suite in a try/except statement executes only if no exceptions are raised by
statements in the try suite.
Short Answer
Q1) Describe the three steps that must be taken when a file is used by a program.
A1) 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
Close file - When the program is finished using the file, the file must be closed.
Q2) Why should a program close a file when it’s finished using it?
A2) Closing a file disconnects the program from the file. In some systems, failure to close an output file
can cause a loss of data
Q3) What is a file’s read position? Where is the read position when a file is first opened for reading?
A3) 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.
Q4) If an existing file is opened in append mode, what happens to the file’s existing contents?
A4) It will not be erased and new data will be written at the end of the file's current contents.
Q5) If a file does not exist and a program attempts to open it in append mode, what happens?