Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python File Handling

Total questions: 15

Worksheet time: 9mins

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.

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+")

8.

The readlines() method returns

a)

str

b)

a list of lines

c)

a list of single characters

d)

a list of integers

9.

An absolute path name begins at the

a)

leaf

b)

stem

c)

root

d)

current directory

10.

What will be the output of the following Python code?

a)

True

b)

False

c)

None

d)

error

11.



(a)  

12.

which function is used to read records from a binary file?

a)

pickle.load(f)

b)

pickle.read()

c)

pickle.open()

d)

pickle.dump(l,f)

13.

Write the appropriate statement in the blank.

(a)  

14.

Write the method to read from a csv file

a)

read()

b)

reader()

c)

readline()

d)

readlines()

15.

Which method is used to write row by row into a csv file along with writer object?

a)

writer()

b)

write()

c)

writeline()

d)

writerow()