wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

File Writing

Total questions: 20

Worksheet time: 2mins

Name
Class
Date
1.

What do you need to import in order to delete a file? (S.C)

a)
os
b)
file
c)
system.os
d)
random
e)
pydoc
2.

Why are with statements useful? (S.C)

a)
They are useless.
b)
You don't have to close the file afterwards.
c)
It creates better files.
d)
You don't have to open the file.
e)
It automatically deletes errors in your file.
3.

What does the 'x' access mode do? (S.C)

a)
creates a file
b)
deletes the file
c)
throws an error if the file already exists
d)
allows you to edit the file
e)
creates a file called 'x'
4.

What does the 'r+' access mode do? (S.C)

a)
allows you to read the file
b)
allows you to create a file
c)
opens the file
d)
allows you to append to the file
e)
allows you to write to the file
5.

Which of the following might need a read only file? (S.C)

a)
amount of inventory a grocery store has
b)
bank account balance
c)
resturaunt menu
d)
Mr. Garza's list of the drumline and their favorite foods
e)
Russell's grades
6.

What is a file? (S.C)

a)
a piece of code used to print out words.
b)
a type of fruit
c)
data that is deleted if a program is not running.
d)
a new type of variable
e)
a place to store data outside the program
7.

What happens if you forget to close the file? (S.C)

a)
Nothing happens.
b)
You don't need to close the file
c)
The file will be deleted.
d)
Changes you make to the file won't be saved.
e)
An error appears.
8.

Can you delete a file? (S.C)

a)
No, files remain forever.
b)
Yes you can.
c)
No, there will be an error if you try to.
d)
Click this answer for +10000 points!!!
e)

Yes, files only exist while the program is running.

9.

What will this code do if there is a file called "food.txt"? (S.C)

a)
Delete "food.txt"
b)
Throw an exception.
c)
Add "Chicken Strips" to "food.txt".
d)
It outputs nothing.
e)
It outputs "The file does not exist.".
10.

What does this code do? (S.C)

a)
Add three names as new lines at the front of the "ListOfNames" file.
b)
Add three names as new lines at the end of the "ListOfNames" file.
c)
Read the "ListOfNames" file and save each line as an item in the list.
d)
Read the "ListOfNames" file and output it as a string.
e)
Print out a list of each line in the file.
11.

How do you delete a file? (M.A)

a)
OS.remove("file.txt")
b)
Import os<br />os.remove("file.txt")
c)
import os<br />os.remove("file.txt")
d)
os.remove("file.txt")
12.

Do you need quotation marks around the access module? (M.A)

a)
Yes, it will throw an error if you don't have them.
b)
No, it will work perfectly without them.
c)
No, it will throw an error if you include them.
d)
Yes, without them the file will be deleted.
13.

What does the access module a+ do? (M.A)

a)
creates or opens a file for writing and reading
b)
creates or opens a file for only writting
c)
creates or opens a file for appending to it
d)
creates or opens a file to appending to it and read
14.

What does the access module w+ do? (M.A)

a)
creates or opens a file for writing and reading
b)
opens a file for reading only
c)
opens a file for reading and writting
d)
creates or opens a file for only writting
15.

Does this code save and write to the file? (M.A)

a)
Yes, it works as intended.
b)
No, it doesn't close the file.
c)
No, with statements don't exist.
d)
Yes, with statements mean all errors are ignored.
16.

Why do you need file writing? (M.A)

a)
It's useless and you don't need it.
b)
You need it to check if your variables are missing.
c)
It is used to store data that you don't need at the moment.
d)
It's easier then writing everything as it's own variable.
17.

What does this code do? (M.A)

a)
writes to the file
b)
creates file called list
c)
closes the file
d)
reads the file
18.

What does .read do? (M.A)

a)
.read puts everything in the file as string.
b)
.read puts the first line in a string.
c)
.read puts each line in a separate string.
d)
.read reads all the text.
19.

How do you close a file? (M.A)

a)
Put file.close at the bottom of the code.
b)
Put file.close() at the bottom of the code.
c)
Put closeFile() at the bottom of the code.
d)
Put file.close() at the top of the code.
e)
Put file.close at the top of the code.
20.

How can you check if a file with a certain name already exist? (M.A)

a)
Use access module x+ with try and except blocks to check for errors saying that the file already exists.
b)
Use access module x with try and except blocks to check for errors saying that the file already exists.
c)
Import OS
d)
Use file.delete.
e)
Use a with statement.