Every problem in the chapter beside the book’s answer, a chapter at a time as you scroll.
Chapter 14: Files
28 problems · 0 figuresConcepts in Practice 14.1.1 §14.1 source ↗
- a.
open("firstfile") - b.
open("firstfile.txt") - c.
fileobj = open("firstfile.txt")
Answer
fileobj = open("firstfile.txt")Concepts in Practice 14.1.2 §14.1 source ↗
open("input.txt")?- a.
Error - b.nothing
- c."input.txt" file is created.
Answer
ErrorConcepts in Practice 14.1.3 §14.1 source ↗
fileobj = open("newfile.txt")
print(fileobj)- a.
Error - b.Information about the object
fileobjis printed. - c.The contents of newfile.txt are printed.
Answer
fileobj is printed.<_io.TextIOWrapper name='newfile.txt' mode='r' encoding='cp1252'>.Concepts in Practice 14.1.4 §14.1 source ↗
Suppose fileobj = open("input.txt") has already been executed for each question below.
input.txt:
Hello world! How are you?What is the correct way to use the
read() function to read the contents of input.txt into a string file_str?- a.
file_str = read(fileobj) - b.
file_str = read("input.txt") - c.
file_str = fileobj.read()
Answer
file_str = fileobj.read()read() function is called on fileobj. The file's contents will be read into file_str.Concepts in Practice 14.1.5 §14.1 source ↗
Suppose fileobj = open("input.txt") has already been executed for each question below.
input.txt:
Hello world! How are you?What is the correct way to use the
readlines() function?- a.
file_lines = readlines(fileobj) - b.
file_lines = fileobj.readlines()
Answer
file_lines = fileobj.readlines()readlines() function is called on fileobj. file_lines is the string list ['Hello world!\n', 'How are you?\n'].Concepts in Practice 14.1.6 §14.1 source ↗
Suppose fileobj = open("input.txt") has already been executed for each question below.
input.txt:
Hello world! How are you?What is printed as a result of executing
print(fileobj.readline())?- a.The first line of input.txt is printed.
- b.The contents of the file input.txt are printed.
- c.
Error
Answer
readline() function reads the next line of a file. Since readline() is called right after opening the file the first line is read. The resulting string "Hello world!" is printed. Note that readline() includes the '\n' character so the print will include an additional newline.Concepts in Practice 14.2.1 §14.2 source ↗
Assume that a file named logfile.txt exists.
To read logfile.txt, which of the following lines of code works best?- a.
logfile = open("logfile.txt") - b.
logfile = open("logfile.txt", 'w') - c.
logfile = open("logfile.txt", 'a')
Answer
logfile = open("logfile.txt")open() function is read.Concepts in Practice 14.2.2 §14.2 source ↗
Assume that a file named logfile.txt exists.
Additions are to be made to logfile.txt. Which of the following lines of code works best?- a.
infile = open("logfile.txt") - b.
infile = open("logfile.txt", 'w') - c.
infile = open("logfile.txt", 'a')
Answer
infile = open("logfile.txt", 'a') 'a' indicates append mode allowing additions to the file.Concepts in Practice 14.2.3 §14.2 source ↗
Assume that a file named logfile.txt exists.
A new file called newlog.txt must be created. Which of the following lines of code works best?- a.
logfile = open("newlog.txt") - b.
logfile = open("newlog.txt", 'w') - c.
logfile = open("newlog.txt", 'a')
Answer
logfile = open("newlog.txt", 'w') 'w' indicates write mode, and newlog.txt will be created or overwritten (if newlog.txt already exists) and will allow writing.Concepts in Practice 14.2.4 §14.2 source ↗
resources.txt already exists. What would happen for each of the following snippets of code?
outfile = open("resources.txt", 'w')
outfile.write("new content")- a.nothing
- b.The contents of the resources.txt file is overwritten with the line
"new content". - c.The resources.txt file is overwritten, but the contents of resources.txt are uncertain.
Answer
close() function must be used.Concepts in Practice 14.2.5 §14.2 source ↗
resources.txt already exists. What would happen for each of the following snippets of code?
outfile = open("resources.txt", 'w')
outfile.write("new content")
outfile.close()- a.The content of the resources.txt file is overwritten with the line
"new content". - b.The line
"new content"is added to the end of resources.txt.
Answer
"new content".close() statement finalizes changes to the file.Concepts in Practice 14.2.6 §14.2 source ↗
resources.txt already exists. What would happen for each of the following snippets of code?
outfile = open("resources.txt", 'a')
outfile.write("new content")
outfile.close()- a.The content of the resources.txt file is overwritten with the line
"new content". - b.The line
"new content"is added to the end of resources.txt.
Answer
"new content" is added to the end of resources.txt.Concepts in Practice 14.3.1 §14.3 source ↗
For each question, assume that the Python file executing the open() function is not in the same folder as the out.txt file.
Each question indicates the location of out.txt, the type of computer, and the desired mode for opening the file. Choose which option is best for opening out.txt.
/users/turtle/files on a Mac for reading- a.
fileobj = open("out.txt") - b.
fileobj = open("/users/turtle/files/out.txt") - c.
fileobj = open("/users/turtle/files/out.txt", 'w')
Answer
fileobj = open("/users/turtle/files/out.txt")Concepts in Practice 14.3.2 §14.3 source ↗
For each question, assume that the Python file executing the open() function is not in the same folder as the out.txt file.
Each question indicates the location of out.txt, the type of computer, and the desired mode for opening the file. Choose which option is best for opening out.txt.
c:\documents\ on a Windows computer for reading- a.
fileobj = open("out.txt") - b.
fileobj = open("c:/documents/out.txt", 'a') - c.
fileobj = open("c:/documents/out.txt")
Answer
fileobj = open("c:/documents/out.txt")Concepts in Practice 14.3.3 §14.3 source ↗
For each question, assume that the Python file executing the open() function is not in the same folder as the out.txt file.
Each question indicates the location of out.txt, the type of computer, and the desired mode for opening the file. Choose which option is best for opening out.txt.
/users/turtle/logs on a Linux computer for writing- a.
fileobj = open("out.txt") - b.
fileobj = open("/users/turtle/logs/out.txt") - c.
fileobj = open("/users/turtle/logs/out.txt", 'w')
Answer
fileobj = open("/users/turtle/logs/out.txt", 'w')Concepts in Practice 14.3.4 §14.3 source ↗
For each question, assume that the Python file executing the open() function is not in the same folder as the out.txt file.
Each question indicates the location of out.txt, the type of computer, and the desired mode for opening the file. Choose which option is best for opening out.txt.
c:\proj\assets on a Windows computer in append mode- a.
fileobj = open("c:\\proj\\assets\\out.txt", 'a') - b.
fileobj = open("c:\proj\assets\out.txt", 'a')
Answer
fileobj = open("c:\\proj\\assets\\out.txt", 'a')Concepts in Practice 14.3.5 §14.3 source ↗
readlines() work for reading the rows in a CSV file?- a.
readlines()reads line by line using the newline\ncharacter. - b.
readlines()is not appropriate for reading a CSV file. - c.
readlines()automatically recognizes a CSV file and works accordingly.
Answer
readlines() reads line by line using the newline \n character.\n character indicates where line breaks are in a file and is used by the readlines() function to tell lines apart.Concepts in Practice 14.3.6 §14.3 source ↗
print(list_csv[1][2])?- a.
532 - b.
268 - c.
Jane Eyre
Answer
268 '268' .Concepts in Practice 14.3.7 §14.3 source ↗
file_obj = open("books.csv")
csv_read = file_obj.readline()
print(csv_read)- a.
['Title, Author, Pages\n', '1984, George Orwell, 268\n', 'Jane Eyre, Charlotte Bronte, 532\n', 'Walden, Henry David Thoreau, 156\n', 'Moby Dick, Herman Melville, 538'] - b.
[['Title', ' Author', ' Pages'], ['1984', ' George Orwell', ' 268'], ['Jane Eyre', ' Charlotte Bronte', ' 532'], ['Walden', ' Henry David Thoreau', ' 156'], ['Moby Dick', ' Herman Melville', ' 538']] - c.
Title, Author, Pages
Answer
Title, Author, Pagescsv_read. The first line is 'Title, Author, Pages' , which is the same as the first row.Concepts in Practice 14.4.1 §14.4 source ↗
What error might occur in each situation?
The line"soft drinks" is changed to "3-4 soft drinks".- a.
FileNotFoundError - b.
IndexError - c.
ValueError
Answer
ValueError "3-4" is not an integer, so the int() function raises a ValueError like before.Concepts in Practice 14.4.2 §14.4 source ↗
What error might occur in each situation?
for line in open("food_order.text"):- a.
FileNotFoundError - b.
IndexError - c.
ValueError
Answer
FileNotFoundError.txt, not .text.Concepts in Practice 14.4.3 §14.4 source ↗
What error might occur in each situation?
parts = line.split() qty = parts[0] item = parts[1]
- a.
FileNotFoundError - b.
IndexError - c.
ValueError
Answer
IndexErrorlen(parts) will be only 1, and parts[1] will be out of range.Concepts in Practice 14.4.4 §14.4 source ↗
For each code snippet, what is the output?
word = "one" try: number = int(word) print(word, "equals", number) except ValueError: print(word, "is not a number")
- a.
one equals 1 - b.
one is not a number - c.
ValueError: invalid literal for int() with base 10: 'one'
Answer
one is not a numberint() function raises a ValueError when trying to convert "one" to an integer, so the program jumps to the except clause.Concepts in Practice 14.4.5 §14.4 source ↗
For each code snippet, what is the output?
word = "one" try: number = int(word) print(word, "equals", number) except IndexError: print(word, "is not a number")
- a.
one equals 1 - b.
one is not a number - c.
ValueError: invalid literal for int() with base 10: 'one'
Answer
ValueError: invalid literal for int() with base 10: 'one'ValueError.Concepts in Practice 14.4.6 §14.4 source ↗
For each code snippet, what is the output?
word = "one"
try:
char = word[3]
print("The char is", char)
except:
print("That didn't work")- a.
The char is e - b.
That didn't work - c.
IndexError: string index out of range
Answer
That didn't workConcepts in Practice 14.5.1 §14.5 source ↗
For each code snippet, what is the output?
p3 = Pizza("M")
print(p3)- a.
Medium pizza for $7.99 - b.
ValueError: Unknown size 'M' - c.
TypeError: missing argument
Answer
Medium pizza for $7.99 "M" is a valid size, so the Pizza object is constructed.Concepts in Practice 14.5.2 §14.5 source ↗
For each code snippet, what is the output?
raise PizzaError("Invalid size")- a.
Invalid size - b.
PizzaError: Invalid size - c.
NameError: name 'PizzaError' is not defined
Answer
NameError: name 'PizzaError' is not definedPizzaError is not a built-in exception type.Concepts in Practice 14.5.3 §14.5 source ↗
For each code snippet, what is the output?
raise ValueError- a.
ValueError - b.
NameError: name 'ValueError' is not defined - c.
SyntaxError: Missing parenthesis in call to 'ValueError'
Answer
ValueErrorLoading chapter 15…
Problems and answers are adapted from Introduction to Python Programming by Udayan Das, Aubrey Lawson, Chris Mayfield and Narges Norouzi, OpenStax, and are used under the CC BY-NC-SA 4.0 licence. Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction. Illustrations: Copyright Rice University, OpenStax, under CC BY-NC-SA 4.0 license. This adaptation is released under the same licence, is non-commercial, and is neither affiliated with nor endorsed by OpenStax or Rice University.