Ch6 Python

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

SIMAD UNIVERSITY ‫جــامــعـــة ســـيــمــد‬

JAAMACADDA SIMAD

October 14, 2021


Name:

Faculty:

Program:

Class:

ID No. :

Phone No. :

Subject : Pearson Starting Out with Python 5rd Tony Gaddis (2021)

October 14, 2021


Prepared by: [email protected] 1
Q1) What is an output file?
October 14, 2021
A1) A file to which a program writes data. It is called an output file because the program sends output to
it.

Q2) What is an input file?

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)?

A7) The file’s contents are erased.

Q8) What is the purpose of opening a file?

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.

Q9) What is the purpose of closing a file?

A9) Closing a file disconnects the program from the file.

October 14, 2021


Prepared by: [email protected] 2
Q10) What is a file’s read position? Initially, where is the read position when an input file is opened?

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?

A11) You open the file in append mode. When14,


October you2021
write data to a file in append mode, the data is
written to the end of the file’s existing contents.

Q12) Write a short program that uses a for loop to write the numbers 1 through 10 to a file.

A12) outfile = open('numbers.txt', 'w')

for num in range(1, 11):

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.

A14) infile = open('numbers.txt', 'r')

line = infile.readline()

while line != '':

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.

A15) infile = open('data.txt', 'r')

for line in infile:

print(line)

infile.close()

October 14, 2021


Prepared by: [email protected] 3
Q16) What is a record? What is a field?

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.

A17) You copy all the original file's records to the


October 14, temporary
2021 file, but when you get to the record that is
to be modified, you do not write its old contents to the temporary 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.

Q19) Briefly describe what an exception is.

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?

A20) The program halts.

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

October 14, 2021


Prepared by: [email protected] 4
Review Questions
Multiple Choice
1. 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

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


October 14, 2021
a. input file b. output file c. sequential access file d. binary file

3. Before a file can be used by a program, it must be…………………….

a. formatted b. encrypted c. closed d. opened

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

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

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

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

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.

a. ordered access b. binary access c. direct access d. sequential access

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

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

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

12. This is a single piece of data within a record.

a. field b. variable c. delimiter d. subrecord

October 14, 2021


Prepared by: [email protected] 5
13. When an exception is generated, it is said to have been __________.

a. built b. raised c. caught d. killed

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

a. exception generator b. exception manipulator c. exception handler d. exception monitor

15. You write this statement to respond to exceptions.

a. run/handle b. try/except c. try/handle d. attempt/except

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

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.

October 14, 2021


Prepared by: [email protected] 6
Closing a file disconnects the file from the program.

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?

A5) The file will be created.

October 14, 2021


Prepared by: [email protected] 7

You might also like