AlgoMaster Logo

File Handling - Quiz

Last Updated: December 6, 2025

File Handling Exercises

29 quizzes

1
Code Completion

Open a text file named notes.txt for reading using the correct mode.

python
1
file_object = open('notes.txt', '')

Click an option to fill the blank:

2
Code Completion

Join a base directory and a filename into one path using the standard library.

python
1
full_path = os.path. (base_dir, 'config.json')

Click an option to fill the blank:

3
Code Completion

Serialize a dictionary as JSON and write it to an open file object.

python
1
json. (settings, config_file)

Click an option to fill the blank:

4
Multiple Choice

Which statement best describes a file in Python?

5
Multiple Choice

Which file mode opens a file for appending new text at the end, creating it if it does not exist?

6
Multiple Choice

Which file method moves the current file position to a specific byte offset?

7
Multiple Choice

You want a path representation that works consistently across platforms and supports methods like .exists(). Which module is designed for this?

8
Multiple Choice

Which os function lists the entries in a directory?

9
Multiple Choice

Which shutil function copies an entire directory tree to a new location?

10
Multiple Choice

Which json function converts a Python dict into a JSON-formatted string?

11
Multiple Choice

You must read a CSV file and access values by column name. Which reader is most convenient?

12
Multiple Choice

Which pickle function reconstructs a Python object from a binary file?

13
Multiple Choice

Which path type describes a path relative to the current working directory?

14
Multiple Choice

What is a safe way to ensure a file is closed after reading?

15
Sequencing

Order the steps to read JSON configuration from a file into a Python dict.

Drag and drop to reorder, or use the arrows.

16
Sequencing

Order the steps to copy a directory to a backup location using shutil.

Drag and drop to reorder, or use the arrows.

17
Output Prediction

What is the output of this code that reads part of a text file?

1content = 'Line1\nLine2\nLine3'
2from io import StringIO
3file_obj = StringIO(content)
4first_line = file_obj.readline().strip()
5print(first_line)
18
Output Prediction

What is the output when using pathlib to get a file name?

1from pathlib import Path
2p = Path('data') / 'sub' / 'values.csv'
3print(p.name)
19
Output Prediction

What will this code print when serializing and deserializing with pickle?

1import pickle
2numbers = [1, 2, 3]
3blob = pickle.dumps(numbers)
4restored = pickle.loads(blob)
5print(len(restored))
20
Bug Spotting

Find the bug in this code that reads a file line by line and counts lines.

Click on the line(s) that contain the bug.

python
1
def count_lines(path):
2
    count = 0
3
    with open(path, 'w') as handler:
4
        for _ in handler:
5
            count += 1
6
    return count
21
Bug Spotting

Find the bug in this code that loads JSON from a file.

Click on the line(s) that contain the bug.

python
1
import json
2
 
3
def load_settings(path):
4
    with open(path, 'r') as config_file:
5
        json.load(path)
6
        return config_file
22
Matching

Match each file-related module to its primary purpose.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

23
Matching

Match each file object method to what it returns or affects.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

24
Fill in the Blanks

Complete the code to read all rows from a CSV file and collect them into a list.

python
1
import csv
2
3
def load_rows(csv_path):
4
with open(csv_path, 'r', newline='') as file_handle:
5
table_reader = csv.(file_handle)
6
all_rows = (table_reader)
7
return all_rows

Click an option to fill blank 1:

25
Fill in the Blanks

Complete the code to create a directory (and parents if needed) using pathlib and check if it exists.

python
1
from pathlib import Path
2
3
def ensure_logs_dir(base_path):
4
logs_path = Path(base_path) / 'logs'
5
logs_path.(parents=True, exist_ok=True)
6
return logs_path.()

Click an option to fill blank 1:

26
Fill in the Blanks

Complete the code to append a log line to a text file in a safe way.

python
1
def append_log(log_path, message):
2
with open(log_path, ) as log_file:
3
log_file.(message + '\n')

Click an option to fill blank 1:

27
Fill in the Blanks

Complete the code to write a Python dict as nicely formatted JSON.

python
1
import json
2
3
def save_profile(profile, json_path):
4
with open(json_path, 'w') as output_file:
5
json.dump(profile, output_file, =)

Click an option to fill blank 1:

28
Hotspot Selection

Click the line that reads the entire file content into memory.

Click on the line to select.

python
1
config_path = 'settings.ini'
2
with open(config_path, 'r') as config_file:
3
    content = config_file.read()
4
print('Loaded configuration')
29
Hotspot Selection

Click the line that uses shutil to move a file to a backup location.

Click on the line to select.

python
1
import shutil
2
 
3
source_file = 'data.db'
4
backup_file = 'backup/data.db'
5
shutil.move(source_file, backup_file)
6
print('Backup complete')

Premium Content

Subscribe to unlock full access to this content and more premium articles.