wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Reading & writing to a text file (Python)

Total questions: 14

Worksheet time: 7mins

Name
Class
Date
1.

This command will open file = open("mytext.txt", "r")

a)

To read the file

b)

To append to the file

c)

To write to the file

2.

This command will open file = open("mytext.txt", "w")

a)

To read the file

b)

To append to the file

c)

To write to the file

3.

If I want to add to the end of a text file, I should:

a)

file = open("file.txt", "w")

b)

file = open("file.txt", "a")

c)

file = open("file.txt", "w+")

d)

file = open("file.txt", "s")

4.

Which of these will generate an error?

a)

file.write("writing")

b)

file..write(writing)

c)

file.write("writing",writing)

d)

file.writetofile()

5.

How do you close the file?

a)

file = close

b)

file.close

c)

close = file()

6.

Why do we close txt files after we are done with them?

a)

This frees up any memory used by having it open

b)

It makes no difference if its closed or not

c)

Once closed, the file cannot be read from or written to without opening it again

d)

It's easier to write to once it's closed

7.

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

a)

Read

b)

Write

c)

Append

d)

None of the mentioned

8.
What is the data type of data read from a file?
a)
String
b)
Integer
c)
Real
d)
Boolean
9.
Which of the following is not a method of opening files?
a)
Replace
b)
Append
c)
Write
d)
Read
10.
Which function is used to read a single line from a file
a)
readline()
b)
readlines()
c)
read()
d)
readfull()
11.
using append overwrites all data in a text file
a)
True
b)
False
12.
Which statement is true
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 reading, if the file does not exist, the program will open an empty file.
d)
None are true
13.

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

a)

myfile.terminate()

b)

myfile.end()

c)

myfile.close()

d)

myfile.remove()

14.

A text file has the file extension of:

a)

.pdf

b)

.txt

c)

.doc

d)

.text