wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python File Handling

Total questions: 41

Worksheet time: 20mins

Name
Class
Date
1.

1. Select the statement to open a file named MYDATA.dat in read binary mode. Let the file object name be myfile.

a)

myfile=open("MYDATA.dat","rb")

b)

myfile=open("MYDATA.dat","r+")

c)

myfile=open("MYDATA.dat","r")

2.

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","__")

a)

r

b)

w

c)

a

d)

r+

3.

3. Choose the correct statement to close a file stream named myfile:

a)

myfile.terminate()

b)

myfile.end()

c)

myfile.close()

d)

myfile.remove()

4.

4. What will be the ouput -

f=open('abc.txt','w')

x=f.read()

f.close()

print(x)

a)

Error(not readable)

b)

Error(not writable)

c)

It will read all the data of the file.

d)

None

5.

5. What will be the output-

f=open('abc.txt','a')

text="xyz"

f.write(text)

f.close()

a)

Writing into the file without erasing old content

b)

Writing into the file by erasing old content

c)

reading from the file without erasing old content

d)

reading from the file by erasing old content

6.

Which function is used to write multiple lines in text file?

a)

write()

b)

writelines()

c)

writeline()

d)

print()

7.

The readlines() method returns

a)

str

b)

a list of lines

c)

a list of single characters

d)

a list of integers

8.

Which of the following command is used to open a file “c:\temp.txt” in read-mode only?

a)

infile = open(“c:\temp.txt”, “r”)

b)

infile = open(“c:\\temp.txt”, “r”)

c)

infile = open(file = “c:\temp.txt”, “r+”)

d)

infile = open(file = “c:\\temp.txt”, “r+”)

9.

Which of the following command is used to open a file “c:\temp.txt” in write-mode only?

a)

outfile = open(“c:\temp.txt”, “w”)

b)

outfile = open(“c:\\temp.txt”, “w”)

c)

outfile = open(file = “c:\temp.txt”, “w+”)

d)

outfile = open(file = “c:\\temp.txt”, “w+”)

10.

Which of the following command is used to open a file “c:\temp.txt” in append-mode?

a)

outfile = open(“c:/temp.txt”, “a”)

b)

outfile = open(“c:\temp.txt”, “w+”)

c)

outfile = open(“c:\\temp.txt”, “a”)

d)

outfile = open(“c:\\temp.txt”, “r+”)

11.

What does the <readlines()> method returns?

a)

str

b)

a list of lines

c)

list of single characters

d)

list of integers

12.

What are the two built-in functions to read a line of text from standard input, which is by default the keyboard?

a)

Input

b)

Read

c)

Scanner

13.

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","__")

a)

r

b)

w

c)

a

d)

r+

14.

Choose the correct statement to close a file stream named myfile:

a)

myfile.terminate()

b)

myfile.end()

c)

myfile.close()

d)

myfile.remove()

15.
Which of the following is not a method of opening files?
a)
Replace
b)
Append
c)
Write
d)
Read
16.
Appending to a file means adding extra data into the file.
a)
True
b)
False
17.
What is the last action that must be performed on a file?
a)
Close
b)
Save
c)
End
d)
Write
18.
What is the data type of data read from a file?
a)
String
b)
Integer
c)
Real
d)
Boolean
19.

What will be the output-

f=open('abc.txt','a')

text="xyz"

f.write(text)

f.close()

a)

Writing into the file without erasing old content

b)

Writing into the file by erasing old content

c)

reading from the file without erasing old content

d)

reading from the file by erasing old content

20.

To open a file poem.txt for reading, the correct syntax is___________

a)

file=open('poem.txt','r')

b)

file=open(file='poem.txt','r')

c)

fileopen=('poem.txt','r')

d)

f=('poem.txt','w')

21.

What is the best way to read an entire file into a single string?

a)

.read()

b)

.readline()

c)

.readlines()

d)

.read(all)

22.

What is the best way to read the first line of a file?

a)

.read()

b)

.readline()

c)

.readlines()

d)

.read(all)

23.

When reading text files, what are the hidden characters typically at the end of each line?

a)

\n

b)

#

c)

@

d)

»

24.

Which is the best type of loop to write a file in?

a)

While loop

b)

for loop

c)

repeat loop

25.

What command can you use to separate parts of a line?

a)

separate()

b)

split()

c)

divide()

d)

cut()

26.

Which file open mode will only allow reading?

a)

w

b)

w+

c)

r

d)

r+

27.

Which file opening mode will overwrite existing content and will not allow reading of the file?

a)

w

b)

w+

c)

r

d)

r+

28.

Which file opening mode will open the file and immediately go to the end of the file to add content?

a)

w

b)

w+

c)

a

d)

r+

29.

Which file opening mode will overwrite existing content and also allow for the reading of content?

a)

w

b)

w+

c)

a

d)

r

30.

Which file opening mode will cause an error if the file cannot be found?

a)

w

b)

w+

c)

a

d)

r

31.

Which file opening mode opens the file as binary data?

a)

"t"

b)

"b"

c)

"bi"

d)

"s"

32.

Which file opening mode is the default mode, that opens a file as text data?

a)

"t"

b)

"b"

c)

"s"

d)

"g"

33.

Which of the following is equivalent to

f = open("demofile.txt")

a)

f = open("demofile.txt", "r+t")

b)

open("demofile.txt", "bt")

c)

open("demofile.txt", "rt")

34.

Which is not a text file?

a)

data.txt

b)

data.csv

c)

data.rft

d)

data.mp3

35.

Which file is not binary file?

a)

student.mp4

b)

student.jpeg

c)

student.csv

d)

student.exe

36.

Which function read single line at a time?

a)

read()

b)

read(n)

c)

readline()

d)

readlines()

37.

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","__")

a)

r

b)

w

c)

a

d)

r+

38.

Which function is used to write multiple lines in text file?

a)

write()

b)

writelines()

c)

lines()

d)

print()

39.

What is the use of “w” in file handling?

a)

read

b)

write

c)

append

d)

None of the above

40.

Which function is used to open a file?

a)

open

b)

open()

c)

opens

d)

opens()

41.

Default file opening mode is __________

a)

write

b)

read and write

c)

read

d)

append