wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python Files

Total questions: 20

Worksheet time: 9mins

Name
Class
Date
1.

Given the following file opened as song_file:


I can't write one song that's not about you

Can't drink without thinkin' about you

Is it too late to tell you that

Everything means nothing if I can't have you?


What is the result of

print(song_file.read(4)) ?

a)

I ca

b)

I can

c)

I c

d)

I can't write one song that's not about you

Can't drink without thinkin' about you

Is it too late to tell you that

Everything means nothing if I can't have you?

2.

Given the following file opened as song_file:


I can't write one song that's not about you

Can't drink without thinkin' about you

Is it too late to tell you that

Everything means nothing if I can't have you?


What is the result of

print(text_file.readline(8)) ?

a)

I can't

b)

I can

c)

I can't write one song that's not about you

d)

Error

3.

Given a text file opened as file_in, which of the following will print the entire file? (Select ALL that apply.)

a)

print(file_in.read())

b)

print(file_in)

c)

lines = file_in.readlines()

for line in lines:

print(line)

d)

lines = file_in.readlines()

for i in range(len(lines)):

print(lines)

e)

for line in file_in:

print(line)

4.

Given the following file opened as song_file:


I can't write one song that's not about you

Can't drink without thinkin' about you

Is it too late to tell you that

Everything means nothing if I can't have you?


What is the result of

print(text_file.readlines()[1]) ?

a)

Can't drink without thinkin' about you

b)

I can't write one song that's not about you

c)

I can't write one song that's not about you

Can't drink without thinkin' about you

Is it too late to tell you that

Everything means nothing if I can't have you?

d)

I

5.

What is the correct way to open the file file_in.txt for reading?

a)

open('file_in.txt', 'r')

b)

open('file_in.txt','r+')

c)

open('file_in.txt','w')

d)

open('file_in.txt','a')

6.

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

a)

.read()

b)

.readline()

c)

.readlines()

d)

.read(all)

7.

What is the best way to read an entire file into a list of strings?

a)

.read()

b)

.readline()

c)

.readlines()

d)

.read(all)

8.

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

a)

.read()

b)

.readline()

c)

.readlines()

d)

.read(all)

9.

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

a)

\n

b)

#

c)

@

d)

»

10.

This command will open MyFile = open('MyText.txt', 'r')

a)

To read the file

b)

To append to the file

c)

To write to the file

11.

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

a)

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

b)

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

c)

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

d)

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

12.

Which of these will generate an error?

a)

MyCipher.write("writing")

b)

MyCipher..write(writing)

c)

MyCipher.write("writing",writing)

d)

MyCipher.readline()

13.

How do you close the file?

a)

MyFile = close()

b)

MyFile.close

c)

close = MyFile()

d)

MyFile.close()

14.

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

a)

While loop

b)

for loop

c)

repeat loop

15.

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

a)

separate()

b)

split()

c)

divide()

d)

cut()

16.

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

a)

myfile.terminate()

b)

myfile.end()

c)

myfile.close()

d)

myfile.remove()

17.

Why is it best to open a file with "with" in Python?

a)

It automatically closes the file

b)

Easier to read the code

c)

Better for binary files

d)

Closers to the pseudocode

18.

How do you read a single line of text from a file?

a)

f.readline()

b)

f.getline()

c)

f.openline()

d)

f.pullline()

19.

What command should you call when you have finished using a text file?

a)

f.close()

b)

f.done()

c)

f.unopen()

d)

f.finished()

20.

See the program and fill in the blank with the correct statement.

(a)