Last Updated: December 6, 2025
29 quizzes
Open a text file named notes.txt for reading using the correct mode.
file_object = open('notes.txt', '')Click an option to fill the blank:
Join a base directory and a filename into one path using the standard library.
full_path = os.path. (base_dir, 'config.json')Click an option to fill the blank:
Serialize a dictionary as JSON and write it to an open file object.
json. (settings, config_file)Click an option to fill the blank:
Which statement best describes a file in Python?
Which file mode opens a file for appending new text at the end, creating it if it does not exist?
Which file method moves the current file position to a specific byte offset?
You want a path representation that works consistently across platforms and supports methods like .exists(). Which module is designed for this?
Which os function lists the entries in a directory?
Which shutil function copies an entire directory tree to a new location?
Which json function converts a Python dict into a JSON-formatted string?
You must read a CSV file and access values by column name. Which reader is most convenient?
Which pickle function reconstructs a Python object from a binary file?
Which path type describes a path relative to the current working directory?
What is a safe way to ensure a file is closed after reading?
Order the steps to read JSON configuration from a file into a Python dict.
Drag and drop to reorder, or use the arrows.
Order the steps to copy a directory to a backup location using shutil.
Drag and drop to reorder, or use the arrows.
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)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)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))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.
def count_lines(path): count = 0 with open(path, 'w') as handler: for _ in handler: count += 1 return countFind the bug in this code that loads JSON from a file.
Click on the line(s) that contain the bug.
import json def load_settings(path): with open(path, 'r') as config_file: json.load(path) return config_fileMatch 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.
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.
Complete the code to read all rows from a CSV file and collect them into a list.
import csvdef load_rows(csv_path): with open(csv_path, 'r', newline='') as file_handle: table_reader = csv.(file_handle) all_rows = (table_reader) return all_rowsClick an option to fill blank 1:
Complete the code to create a directory (and parents if needed) using pathlib and check if it exists.
from pathlib import Pathdef ensure_logs_dir(base_path): logs_path = Path(base_path) / 'logs' logs_path.(parents=True, exist_ok=True) return logs_path.()Click an option to fill blank 1:
Complete the code to append a log line to a text file in a safe way.
def append_log(log_path, message): with open(log_path, ) as log_file: log_file.(message + '\n')Click an option to fill blank 1:
Complete the code to write a Python dict as nicely formatted JSON.
import jsondef save_profile(profile, json_path): with open(json_path, 'w') as output_file: json.dump(profile, output_file, =)Click an option to fill blank 1:
Click the line that reads the entire file content into memory.
Click on the line to select.
config_path = 'settings.ini'with open(config_path, 'r') as config_file: content = config_file.read()print('Loaded configuration')Click the line that uses shutil to move a file to a backup location.
Click on the line to select.
import shutil source_file = 'data.db'backup_file = 'backup/data.db'shutil.move(source_file, backup_file)print('Backup complete')