──────────────────────────────────────

71. Make Your Own Python Tool · Preview Safely and Write a Run Guide

Demonstrate a safe default and give someone repeatable run instructions.

Lesson files · lessons/make-your-own-tool/preview-and-handover

Before you start
- Show the proposed effects of a tool in a dry run.
- Use exclusive creation to preserve an existing report and read text back.

By the end
- Demonstrate preview, first write and repeat-write protection using the three modes.
- Give a partner a run guide with an input, command, expected result and recovery step.

Where you can use it
Give another volunteer a command they can run to preview a report, then show them how to save it while preserving their previous copy.

Preview mode shows what the tool plans to do. On your computer, the capstone can display the report first and wait for an output path before creating a file. In the Studio starter, work inside the supplied temporary folder. report.is_file() tells you whether report.txt exists. Converting that answer with int() gives a count of zero or one.

A run guide is for someone new to the program. Give them the working folder and input filename, one command they can copy, a result they can check and one recovery step. If a report already exists, show them how to choose a new output filename.

Predict
Someone only wants to see today’s report. What should the default do?
- Print it and leave the folder alone.
- Write over the last report and then print.

Worked example (example.py)

def describe_mode(output_path):
    if output_path is None:
        return "Preview on screen"
    return f"Create {output_path}"

print(describe_mode(None))


Example result
Preview on screen

Use the starter’s temporary folder. For preview, print plan: create report.txt and files: 0. For write, create report.txt in x mode and print saved: report.txt and files: 1. For repeat, create the file first and try to save a second report there. Catch FileExistsError and print kept: original and files: 1. Read the file to check its contents and use report.is_file() for the count.

Write a short run guide for your Weekly Activity Digest. Ask a partner to follow it in a practice folder and note the first step they ask you to clarify.

Starter program (starter.py)

from pathlib import Path
from tempfile import TemporaryDirectory

mode = input().strip()
with TemporaryDirectory() as folder:
    report = Path(folder) / "report.txt"
    print("Ready")


Starter input
preview

Hint
Use a branch per mode. Put the existing-file check inside the second write and print the count after handling the mode.

Case 1. Try the sample
Input
preview
Expected output
plan: create report.txt
files: 0

Case 2. Write on request
Input
write
Expected output
saved: report.txt
files: 1

Case 3. Run again
Input
repeat
Expected output
kept: original
files: 1

Stretch
Revise the step where your partner hesitated. Ask them to use the revised guide with a different activity export.

Reflect
How would a person know whether a failed second run changed their old report?

Check your prediction
Print it and leave the folder alone. The person chooses when to create an output file.
