wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

File Handling in Python

Total questions: 15

Worksheet time: 8mins

Name
Class
Date
1.

Q1. 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

I c

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

Can't drink without thinkin' about

c)

I c

d)

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.readlines()[1]) ?

a)

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

b)

Can't drink without thinkin' about you

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

Can't drink without thinkin' about

c)

you

Is it too late to tell you that

Everything means nothing if I can't have you?

d)

I

3.

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

a)

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

b)

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

c)

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

d)

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

4.

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

a)

.read()

b)

.readline()

c)

.readlines()

d)

.read(all)

5.

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

a)

.read()

b)

.readline()

c)

.readlines()

d)

.read(all)

6.

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

a)

.read()

b)

.readline()

c)

.readlines()

d)

.read(all)

7.

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

a)

\n

b)

»

c)

#

d)

@

8.

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

a)

To write to the file

b)

To read the file

c)

To append to the file

9.

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

10.

Which of these will generate an error?

a)

MyCipher.write("writing")

b)

MyCipher..write(writing)

c)

MyCipher.write("writing",writing)

d)

MyCipher.readline()

11.

How do you close the file?

a)

MyFile = close()

b)

MyFile.close

c)

close = MyFile()

d)

MyFile.close()

12.

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

a)

While loop

b)

for loop

c)

repeat loop

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.

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

15.

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())

lines = file_in.readlines()

b)

print(file_in)

lines = file_in.readlines()

c)

for line in lines:

print(line)

d)

for i in range(len(lines)):

print(lines)

e)

for line in file_in:

print(line)