Lab, Files and Data Formats
7 tasks, easy to hard: Predict, then run, Write it, Complete it, Fix it, Trace it, Refactor it, Mini challenge. Hints come one at a time and you can see each answer. Lab work is saved as practice and earns no XP.
Open the lessonYou can read every task below. Running, checking and saving them needs a free ID.
7 tasks
Task 01 .Type what the program will print, then run it to compare.
Type exactly what this program prints.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 02 .Solve it from the prompt, starting from an empty editor.
Using pathlib, write the text Feed the cat followed by a newline to todo.txt with write_text, then print the file's contents without the newline, and print whether it is a file.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 03 .Fill the marked gaps in the starter code.
Replace ___ so target is the file june.csv inside reports/2024, built with the / operator.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 04 .The starter code has a bug. Repair it so the checks pass.
The program sets up its output folder twice, as happens when it is run again, and should print True. It raises FileExistsError. Fix the mkdir calls.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 05 .Fill the table with the variable values, step by step.
Each row shows name and count each time one of them changes, once both exist. The loop visits the matches in sorted order.
from pathlib import Path
box = Path("box")
box.mkdir()
for n in ["b.txt", "a.txt", "c.log"]:
(box / n).write_text("x", encoding="utf-8")
count = 0
for path in sorted(box.glob("*.txt")):
name = path.name
count = count + 1Sign in or create a free ID to fill in and check this table.
Task 06 .Rewrite working code to the stated rule, keeping the same output. Rule: Build the path with Path and /, and read and write with write_text and read_text: no open( and no string + "/".
This code works but builds paths by joining strings. Rewrite it with Path, the / operator and write_text/read_text, with no open( calls and no + "/". The output must not change.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 07 .A short task that combines this lesson with earlier ones.
The starter creates a folder of files. Write by_suffix(folder), which returns a dictionary from each lower-case suffix to a sorted list of the stems with that suffix. Print the suffixes in sorted order with their stems, then the sorted names of the images (.jpg and .png, in any case). The output should be:
.jpg ['beach', 'sunset'] .png ['logo'] .txt ['notes'] ['beach.JPG', 'logo.png', 'sunset.jpg']
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.