wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

File Handling In Python

Total questions: 20

Worksheet time: 9mins

Name
Class
Date
1.

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

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.

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

a)

myfile.terminate()

b)

myfile.end()

c)

myfile.close()

d)

myfile.remove()

4.

Fill in the blank with appropriate statement.

(a)  

5.

Write the appropriate statement in the blank.

(a)  

6.

Which statement(s) is/are true to open a binary file "Record.dat" for reading and writing.

a)

file =open("Record.dat","wb")

b)

file =open("Record.dat","rb")

c)

file =open("Record.dat","ab")

d)

file =open("Record.dat","rb+")

7.

Which function read single line at a time?

a)

read()

b)

read(n)

c)

readline()

d)

readlines()

8.

Which file is not binary file?

a)

student.mp4

b)

student.jpeg

c)

student.csv

d)

student.exe

9.

Where files are store permanently?

a)

RAM

b)

Hard Disk

c)

Cache memory

d)

None of these

10.

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

a)

write()

b)

writelines()

c)

lines()

d)

print()

11.
Appending to a file means adding extra data into the file.
a)
True
b)
False
12.

Which statement(s) is/are true to open a binary file "Record.dat" for reading and writing ?

a)

file =open("Record.dat","wb")

b)

file =open("Record.dat","rb")

c)

file =open("Record.dat","ab")

d)

file =open("Record.dat","rb+")

13.

Q.2 Which of the following statements are not true regarding the opening modes of a file?

a)

When you open a file for reading, if the file does not exist, an error occurs.

b)

When you open a file for writing, if the file does not exist, an error occurs

c)

When you open a file for writing, if the file does not exist, a new file is created.

d)

When you open a file for writing, if the file exists, the existing file is overwritten with the new file.

14.

Q.3 Which module is used to work with binary files?

a)

CSV module

b)

pickle module

c)

math module

d)

binary module

15.

Q.5 'try' block is used to

a)

execute a code

b)

through the exceptions

c)

to try some random code

d)

to give errors

16.

Function used to write data in binary format

a)

write()

b)

output()

c)

load()

d)

dump()

17.
Which of the following is not a method of opening files?
a)
Replace
b)
Append
c)
Write
d)
Read
18.
What is the data type of data read from a file?
a)
String
b)
Integer
c)
Real
d)
Boolean
19.
Reading from a file often involves using...
a)
A loop
b)
An condition
c)
Comments
20.

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

a)

Read

b)

Write

c)

Append

d)

None of the mentioned