WorksheetsPython File Handling
Total questions: 41
Worksheet time: 20mins
1. Select the statement to open a file named MYDATA.dat in read binary mode. Let the file object name be myfile.
myfile=open("MYDATA.dat","rb")
myfile=open("MYDATA.dat","r+")
myfile=open("MYDATA.dat","r")
2. Mode in the following program to open file for writing in a mode where new data will be added at the end of old data
file =open("poem.txt","__")
r
w
a
r+
3. Choose the correct statement to close a file stream named myfile:
myfile.terminate()
myfile.end()
myfile.close()
myfile.remove()
4. What will be the ouput -
f=open('abc.txt','w')
x=f.read()
f.close()
print(x)
Error(not readable)
Error(not writable)
It will read all the data of the file.
None
5. What will be the output-
f=open('abc.txt','a')
text="xyz"
f.write(text)
f.close()
Writing into the file without erasing old content
Writing into the file by erasing old content
reading from the file without erasing old content
reading from the file by erasing old content
Which function is used to write multiple lines in text file?
write()
writelines()
writeline()
print()
The readlines() method returns
str
a list of lines
a list of single characters
a list of integers
Which of the following command is used to open a file “c:\temp.txt” in read-mode only?
infile = open(“c:\temp.txt”, “r”)
infile = open(“c:\\temp.txt”, “r”)
infile = open(file = “c:\temp.txt”, “r+”)
infile = open(file = “c:\\temp.txt”, “r+”)
Which of the following command is used to open a file “c:\temp.txt” in write-mode only?
outfile = open(“c:\temp.txt”, “w”)
outfile = open(“c:\\temp.txt”, “w”)
outfile = open(file = “c:\temp.txt”, “w+”)
outfile = open(file = “c:\\temp.txt”, “w+”)
Which of the following command is used to open a file “c:\temp.txt” in append-mode?
outfile = open(“c:/temp.txt”, “a”)
outfile = open(“c:\temp.txt”, “w+”)
outfile = open(“c:\\temp.txt”, “a”)
outfile = open(“c:\\temp.txt”, “r+”)
What does the <readlines()> method returns?
str
a list of lines
list of single characters
list of integers
What are the two built-in functions to read a line of text from standard input, which is by default the keyboard?
Input
Read
Scanner
Mode in the following program to open file for writing in a mode where new data will be added at the end of old data
file =open("poem.txt","__")
r
w
a
r+
Choose the correct statement to close a file stream named myfile:
myfile.terminate()
myfile.end()
myfile.close()
myfile.remove()
What will be the output-
f=open('abc.txt','a')
text="xyz"
f.write(text)
f.close()
Writing into the file without erasing old content
Writing into the file by erasing old content
reading from the file without erasing old content
reading from the file by erasing old content
To open a file poem.txt for reading, the correct syntax is___________
file=open('poem.txt','r')
file=open(file='poem.txt','r')
fileopen=('poem.txt','r')
f=('poem.txt','w')
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 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
#
@
»
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()
Which file open mode will only allow reading?
w
w+
r
r+
Which file opening mode will overwrite existing content and will not allow reading of the file?
w
w+
r
r+
Which file opening mode will open the file and immediately go to the end of the file to add content?
w
w+
a
r+
Which file opening mode will overwrite existing content and also allow for the reading of content?
w
w+
a
r
Which file opening mode will cause an error if the file cannot be found?
w
w+
a
r
Which file opening mode opens the file as binary data?
"t"
"b"
"bi"
"s"
Which file opening mode is the default mode, that opens a file as text data?
"t"
"b"
"s"
"g"
Which of the following is equivalent to
f = open("demofile.txt")
f = open("demofile.txt", "r+t")
open("demofile.txt", "bt")
open("demofile.txt", "rt")
Which is not a text file?
data.txt
data.csv
data.rft
data.mp3
Which file is not binary file?
student.mp4
student.jpeg
student.csv
student.exe
Which function read single line at a time?
read()
read(n)
readline()
readlines()
Mode in the following program to open file for writing in a mode where new data will be added at the end of old data
file =open("poem.txt","__")
r
w
a
r+
Which function is used to write multiple lines in text file?
write()
writelines()
lines()
print()
What is the use of “w” in file handling?
read
write
append
None of the above
Which function is used to open a file?
open
open()
opens
opens()
Default file opening mode is __________
write
read and write
read
append
