20 questions
Given the following file opened as song_file:
I can't write one song that's not about you
Can't drink without thinkin' about you
Is it too late to tell you that
Everything means nothing if I can't have you?
What is the result of
print(song_file.read(4)) ?
I ca
I can
I c
I can't write one song that's not about you
Can't drink without thinkin' about you
Is it too late to tell you that
Everything means nothing if I can't have you?
Given the following file opened as song_file:
I can't write one song that's not about you
Can't drink without thinkin' about you
Is it too late to tell you that
Everything means nothing if I can't have you?
What is the result of
print(text_file.readline(8)) ?
I can't
I can
I can't write one song that's not about you
Error
Given a text file opened as file_in, which of the following will print the entire file? (Select ALL that apply.)
print(file_in.read())
print(file_in)
lines = file_in.readlines()
for line in lines:
print(line)
lines = file_in.readlines()
for i in range(len(lines)):
print(lines)
for line in file_in:
print(line)
Given the following file opened as song_file:
I can't write one song that's not about you
Can't drink without thinkin' about you
Is it too late to tell you that
Everything means nothing if I can't have you?
What is the result of
print(text_file.readlines()[1]) ?
Can't drink without thinkin' about you
I can't write one song that's not about you
I can't write one song that's not about you
Can't drink without thinkin' about you
Is it too late to tell you that
Everything means nothing if I can't have you?
I
What is the correct way to open the file file_in.txt for reading?
open('file_in.txt', 'r')
open('file_in.txt','r+')
open('file_in.txt','w')
open('file_in.txt','a')
What is the best way to read an entire file into a single string?
.read()
.readline()
.readlines()
.read(all)
What is the best way to read an entire file into a list of strings?
.read()
.readline()
.readlines()
.read(all)
What is the best way to read the first line of a file?
.read()
.readline()
.readlines()
.read(all)
When reading text files, what are the hidden characters typically at the end of each line?
\n
#
@
»
This command will open MyFile = open('MyText.txt', 'r')
To read the file
To append to the file
To write to the file
If I want to add to the end of a text file, I should:
MyCipher = open("MyCipher.txt", "w")
MyCipher = open("MyCipher.txt", "a")
MyCipher = open("MyCipher.txt", "w+")
MyCipher = open("MyCipher.txt", "s")
Which of these will generate an error?
MyCipher.write("writing")
MyCipher..write(writing)
MyCipher.write("writing",writing)
MyCipher.readline()
How do you close the file?
MyFile = close()
MyFile.close
close = MyFile()
MyFile.close()
Which is the best type of loop to write a file in?
While loop
for loop
repeat loop
What command can you use to separate parts of a line?
separate()
split()
divide()
cut()
Choose the correct statement to close a file stream named myfile:
myfile.terminate()
myfile.end()
myfile.close()
myfile.remove()
Why is it best to open a file with "with" in Python?
It automatically closes the file
Easier to read the code
Better for binary files
Closers to the pseudocode
How do you read a single line of text from a file?
f.readline()
f.getline()
f.openline()
f.pullline()
What command should you call when you have finished using a text file?
f.close()
f.done()
f.unopen()
f.finished()
See the program and fill in the blank with the correct statement.