Skip to main content
Contents Index
Search Book
Search Results:
No results.
Readability settings Prev Up Next Scratch ActiveCode Profile
title here
\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 8.12 Multiple Choice Questions
Activity 8.12.1 .
Which of the following can be used to open a file called
myText.txt in read-only mode?
infile = open("myText.txt", โwโ)
This will open in write-only mode.
infile = open("myText.txt", r)
The r must be in quotes.
infile = open("myText.txt", โreadโ)
Use "r" for read-only mode.
infile = open("myText.txt")
The default is read if no mode is specified
Activity 8.12.2 .
Which of the following can be used to open a file called
myText.txt in write-only mode?
outfile = open("myText.txt", w)
The w must be in quotes.
outfile = open("myText.txt", โwriteโ)
Use "w" for write-only mode.
outfile = open("myText.txt", โwโ)
This will open the file in write-only mode.
outfile = open("myText.txt")
This would open the file in read-only mode.
Activity 8.12.3 .
Which command below closes the already open file
myText.txt if the following code has already been written?
ref_file = open("myText.txt", "r")
close()
You must call this method on the file object.
ref_file.close()
This closes the file using the variable it has been assigned to.
close(ref_file)
The command close() needs to be called on the file object using dot notation.
close("myText")
The command close() needs to be called on the file object using dot notation.
Activity 8.12.4 .
Which of the commands below is used to add the following string to the end of a file object
filevar?
somestring = "my Sentence"
filevar.append(somestring)
Append is a command used for lists, not files.
filevar.write("somestring")
somestring is a variable and does not need quotation marks around it.
filevar.write(somestring)
Using dot notation, we can call the write command with the string variable inside the parentheses.
somestring.write()
The command write() needs to be called on the file object, not the string itself.
Activity 8.12.5 .
The contents of
names.txt is listed here:
Moana
Cinderella
Tiana
Which of the following code blocks will print all of the names in
names.txt?
names = open("names.txt", "r")
for line in names:
print(names)
names = open("names.txt", "r")
for line in names:
print(line)
names = open("names.txt", "r")
for line in names:
print("line")
I
Append is a command used for lists, not files.
II
somestring is a variable and does not need quotation marks around it.
III
Using dot notation, we can call the write command with the string variable inside the parentheses.
None of the above.
Activity 8.12.6 .
What keyword do you use to specify code to execute if there is an error when the body of the
try executes?
catch
This is used in other languages, but not in Python
except
Use except to execute code when an error occurs.
exception
Close, but it is shorter.
error
Try again.
Activity 8.12.7 .
How many errors are in the code below? It should open the file in read-only mode, read each line and print each line and then close the file.
def print_contents(file)
file_obj = open(file)
for line in "file_obj":
print(line_obj)
1
Try again
2
Try again
3
Try again
4
The first line is missing a ":", the third shouldnโt have file_obj in quotes, the fourth should use line rather than line_obj, and the close is missing.
You have attempted
of
activities on this page.