wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python File Handling

Total questions: 39

Worksheet time: 20mins

Name
Class
Date
1.

In Python, which of the following is NOT a file operation you can perform?

a)

Open a file

b)

Read or write data to / from a file

c)

Delete a file

d)

Close the file

e)

Read records into lists

2.

In Python, the open() function takes the ______ of the file and an optional ______ as parameters.

a)

name, file mode

b)

size, encoding

c)

type, path

d)

location, extension

3.

What is the default mode when opening a file using open(filename) in Python?

a)

'w' (write)

b)

'r' (read)

c)

'a' (append)

d)

'w+' (read and write)

4.

Which code snippet opens a file for both reading and writing in Python?

a)

inputfile = open(filename)

b)

inputfile = open(filename, 'w')

c)

inputfile = open(filename, 'w+')

d)

inputfile = open(filename, 'r')

5.

What does the 'w' mode do when opening a file?

a)

Opens the file for reading

b)

Opens the file for writing

c)

Opens the file for appending

d)

Opens the file for both reading and writing

6.

Which file mode in Python allows you to read from a file and sets the initial position at the beginning?

a)

r

b)

w

c)

a

d)

w+

7.

Which file mode in Python allows you to write to a file and truncates the file if it already exists?

a)

r

b)

w

c)

a

d)

r+

8.

Which file mode in Python allows you to append to a file and sets the initial position at the end?

a)

r

b)

w

c)

a

d)

r+

9.

Which file mode in Python allows both reading and writing, and truncates the file if it already exists?

a)

r+

b)

w+

c)

a+

d)

r

10.

Which file mode in Python allows both reading and writing, and sets the initial position at the beginning?

a)

r+

b)

w+

c)

a+

d)

a

11.

Which file mode in Python allows both reading and appending, and sets the initial position at the end?

a)

r+

b)

w+

c)

a+

d)

w

12.

Fill in the blank: Truncate refers to a process or operation that ______ or cuts off a piece of data, such as a string or a file, by removing characters, digits, or bytes from the end.

a)

shortens

b)

expands

c)

encrypts

d)

duplicates

13.

Which Python module provides a way of accessing and interacting with the operating system?

a)

sys

b)

os

c)

math

d)

random

14.

The os.path is a submodule of the ______ module.

a)

os

b)

sys

c)

pathlib

d)

shutil

15.

What does the isfile() function in the os.path module return?

a)

The size of the file

b)

True or False depending on whether a file exists

c)

The contents of the file

d)

The file extension

16.

In the code example, what will be printed if the file 'data.txt' does not exist?

a)

The file data.txt does not exist, creating the file...

b)

Error: File not found.

c)

File opened successfully.

d)

No output will be printed.

17.

To check if a file exists in Python, you need to:
1. import the os.path module into your program
2. use the ______ function.

a)

isfile()

b)

exists()

c)

checkfile()

d)

fileexists()

18.

What does the write() function do in Python file handling?

a)

Writes a single line to the text file

b)

Writes multiple lines to a file

c)

Reads a single line from a file

d)

Reads the entire file

19.

What is the purpose of the writelines() function in Python file handling?

a)

Write a single line to the text file

b)

Write multiple lines to a file (a list)

c)

Read a single line from a file

d)

Read all lines from a file

20.

Fill in the blank: In Python, a file handle is like a ______ that points to a position in a file where data will be written to or read from.

a)

cursor

b)

window

c)

pointer

d)

index

21.

Which seek() parameter sets the reference point at the beginning of the file?

a)

0

b)

1

c)

2

d)

None of the above

22.

Fill in the blank: The seek() function in Python file handling is used to reset the file handle to the ______ of the file.

a)

beginning

b)

end

c)

middle

d)

current position

23.

What does the read() function do in Python file handling?

a)

Reads the entire file and returns the file content in a STRING

b)

Reads a line of the file and returns the line in a STRING

c)

Reads all the lines of the file and returns the lines in a LIST

d)

Writes a line to the file

24.

Which function reads a line of the file and returns the line in a STRING?

a)

read()

b)

readline()

c)

readlines()

d)

writelines()

25.

Which function reads all the lines of the file and returns the lines in a LIST?

a)

read()

b)

readline()

c)

readlines()

d)

write()

26.

What does the close() function do in Python file handling?

a)

A) Opens a file

b)

B) Closes an open file

c)

C) Reads a file

d)

D) Writes to a file

27.

Why is it good practice to always close a file that has been opened in Python?

a)

Because changes made to a file may not be applied until the file is closed.

b)

Because closing a file increases the speed of the program.

c)

Because closing a file automatically deletes the file.

d)

Because closing a file prevents syntax errors.

28.

What is the main advantage of using the with statement when working with files in Python?

a)

It automatically closes the file

b)

It opens multiple files

c)

It writes data to the file

d)

It reads data from the file

29.

Fill in the blank: The following Python list contains the names of three TV shows: ["Big Bang Theory\n", "Greys Anatomy\n", "Breaking Bad\n"]. The second TV show in the list is _______.

a)

Greys Anatomy

b)

Friends

c)

The Office

d)

Game of Thrones

30.

What does the 'w+' mode do when opening a file in Python?

a)

It overwrites the file if it already exists and allows both writing and reading.

b)

It opens the file for reading only and throws an error if the file does not exist.

c)

It appends to the file if it exists and allows both writing and reading.

d)

It opens the file for writing only and does not allow reading.

31.

Fill in the blank: To append data to a file instead of overwriting it, you should use the mode "___" when opening the file in Python.

a)

a+

b)

w

c)

r

d)

w+

32.

What function can be used to read all the lines from a text file and store them as a list in Python?

a)

readlines()

b)

read()

c)

write()

d)

writelines()

33.

Which of the following statements about CSV (Comma Separated Values) files is true?

a)

Each line of the file is a data record

b)

Each record consists of one or more fields, separated by commas

c)

Both A and B

d)

None of the above

34.

What is the ID of Bobby Berry?

a)

39743

b)

58291

c)

76420

d)

18356

35.

What is the name of the student with ID 20393?

a)

Colin Cherry

b)

John Smith

c)

Emily Watson

d)

Sarah Lee

36.

For working with CSV files in Python, there is an inbuilt module called csv that we can import into our programs. The following code uses the csv function reader() to read each line from the file, split the line according to the position of the comma, and add the data to a list named readerList. Which function is used to read and split lines from a CSV file in Python?

a)

open()

b)

reader()

c)

split()

d)

append()

37.

For working with CSV files in Python, there is an inbuilt module called csv that we can import into our programs. The following code uses the csv function reader() to read each line from the file, split the line according to the position of the comma, and add the data to a list named readerList. What does the code print when executing the section: for row in studentList: print(row[0])

a)

All student IDs

b)

All student names

c)

All student names and IDs

d)

Nothing

38.

Explain the purpose of the studentList in the provided code.

a)

It stores the list of students in the code.

b)

It calculates the total marks of students.

c)

It displays the student names on the screen.

d)

It sorts the students by their grades.

39.

What does the following Python code do? # Print names and IDs together print("\nPrinting all names and IDs:") for row in studentList : print(row[0], row[1])

a)

It prints only the names from studentList

b)

It prints only the IDs from studentList

c)

It prints both names and IDs from studentList

d)

It adds new names and IDs to studentList