Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Python Programming: File Handling - Chapter 7 Questions & Answers, Exams of Nursing

A series of questions and answers related to file handling in python, covering concepts like file storage, file handles, opening files for reading and writing, and common file handling operations. It's a valuable resource for students learning python programming, particularly those working through the py4e course materials.

Typology: Exams

2024/2025

Available from 02/13/2025

lisa-writer
lisa-writer 🇺🇸

815 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Py4e: Chapter 7 Questions & Answers
Given the architecture and terminology we introduced in Chapter 1, where are files
stored? - ANSWERSSecondary memory
What is stored in a "file handle" that is returned from a successful open() call? -
ANSWERSThe handle is a connection to the file's data
What do we use the second parameter of the open() call to indicate? -
ANSWERSWhether we want to read data from the file or write data to the file
What Python function would you use if you wanted to prompt the user for a file name to
open? - ANSWERSinput( )
What is the purpose of the newline character in text files? - ANSWERSIt indicates the
end of one line of text and the beginning of another line of text
If we open a file as follows:
xfile = open('mbox.txt') - ANSWERSfor line in xfile:
What is the purpose of the following Python code?
fhand = open('mbox.txt')
x = 0
for line in fhand:
x = x + 1
print(x) - ANSWERSCount the lines in the file 'mbox.txt'
If you write a Python program to read a text file and you see extra blank lines in the
output that are not present in the file input as shown below, what Python string function
will likely solve the problem?
From: stephen.marquard@uct.ac.za
From: louis@media.berkeley.edu
From: zqian@umich.edu
From: rjlowe@iupui.edu
... - ANSWERSrstrip( )
The following code sequence fails with a traceback when the user enters a file that does
not exist. How would you avoid the traceback and make it so you could print out your
own error message when a bad file name was entered?
fname = input('Enter the file name: ')
pf2

Partial preview of the text

Download Python Programming: File Handling - Chapter 7 Questions & Answers and more Exams Nursing in PDF only on Docsity!

Py4e: Chapter 7 Questions & Answers

Given the architecture and terminology we introduced in Chapter 1, where are files stored? - ANSWERSSecondary memory What is stored in a "file handle" that is returned from a successful open() call? - ANSWERSThe handle is a connection to the file's data What do we use the second parameter of the open() call to indicate? - ANSWERSWhether we want to read data from the file or write data to the file What Python function would you use if you wanted to prompt the user for a file name to open? - ANSWERSinput( ) What is the purpose of the newline character in text files? - ANSWERSIt indicates the end of one line of text and the beginning of another line of text If we open a file as follows: xfile = open('mbox.txt') - ANSWERSfor line in xfile: What is the purpose of the following Python code? fhand = open('mbox.txt') x = 0 for line in fhand: x = x + 1 print(x) - ANSWERSCount the lines in the file 'mbox.txt' If you write a Python program to read a text file and you see extra blank lines in the output that are not present in the file input as shown below, what Python string function will likely solve the problem? From: stephen.marquard@uct.ac.za From: louis@media.berkeley.edu From: zqian@umich.edu From: rjlowe@iupui.edu ... - ANSWERSrstrip( ) The following code sequence fails with a traceback when the user enters a file that does not exist. How would you avoid the traceback and make it so you could print out your own error message when a bad file name was entered? fname = input('Enter the file name: ')

fhand = open(fname) - ANSWERStry / except What does the following Python code do? fhand = open('mbox-short.txt') inp = fhand.read() - ANSWERSReads the entire file into the variable inp as a string What is the purpose of the newline character in text files? - ANSWERSIt indicates the end of one line of text and the beginning of another line of text