Py4e: Chapter 7

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

If we open a file as follows: xfile = open('mbox.txt')

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

Count the lines in the file 'mbox.txt'

What is the purpose of the newline character in text files?

It indicates the end of one line of text and the beginning of another line of text

What does the following Python code do? fhand = open('mbox-short.txt') inp = fhand.read()

Reads the entire file into the variable inp as a string

Given the architecture and terminology we introduced in Chapter 1, where are files stored?

Secondary memory

What is stored in a "file handle" that is returned from a successful open() call?

The handle is a connection to the file's data

What do we use the second parameter of the open() call to indicate?

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

input( )

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: [email protected] From: [email protected] From: [email protected] From: [email protected] ...

rstrip( )

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)

try / except


Set pelajaran terkait

Chapter 4- The Great Lakes: The U.S. and Canada's Freshwater Treasures

View Set

Chapter 54 International and World Trade Law

View Set

Chapter 13: Outcome Identification and Planning

View Set

010.1-A. Countries and nationalities. I am from ___________. I am ________________.

View Set

Angle Relationships, and solving for x

View Set

Chapter 23: Auto Insurance and Society

View Set