Work & Development · Python & Automation
Free preview
Define a Bounded File Workspace
Lesson 1 of Python for Everyday Automation. Anyone can read this lesson with no account. Every lesson after this one is for students.
Repeatable file tasks
Choose which files a script may read and check that each path stays in the practice folder.
1Start with one idea
Automation means letting a script repeat a task. A workspace is the folder it is allowed to use. A path gives the location of a file or folder. Before working quickly, make sure the script chooses the right files. Think of a class helper told to collect only blue worksheets from one tray, not papers from every desk.
2Select files deliberately
Automation repeats a decision quickly. If the decision selects the wrong files, speed makes the mistake larger. Begin with a dedicated workspace containing fictional inputs and a separate reports folder. Write down which file endings and folders are allowed before writing an output. A directory is a folder. First print a list of the selected files so you can check the selection without changing their content.
Resolve a path before checking where it leads. This turns it into a full path and follows symbolic links. .. means “go up one folder.” A symbolic link is a filesystem link to another location, which might be outside the workspace. Check the resolved folder relationships, not just the first letters of the filename. This example creates its own temporary practice folder and leaves your personal folders alone.
3Worked example
from pathlib import Path
from tempfile import TemporaryDirectory
def inside(base, candidate):
base = base.resolve()
candidate = candidate.resolve()
if candidate == base or base not in candidate.parents:
raise ValueError("Expected a child path inside the workspace")
return candidate
with TemporaryDirectory() as folder:
base = Path(folder).resolve()
data = base / "data"
data.mkdir()
(data / "sessions.csv").write_text("event,attendees,minutes\nSkills,4,30\n", encoding="utf-8")
(data / "notes.txt").write_text("Fictional fixture only\n", encoding="utf-8")
selected = [inside(base, path) for path in sorted(data.glob("*.csv")) if path.is_file()]
print([str(path.relative_to(base)) for path in selected])
try:
inside(base, base / ".." / "outside.csv")
except ValueError:
print("Blocked a path outside the workspace")
Expect a list containing data/sessions.csv, with the platform's path separator, followed by Blocked a path outside the workspace. notes.txt was not selected. A fixture is made-up sample data for checking the program. These sample writes happen in a new folder created by TemporaryDirectory. Use this setup for the exercise; do not apply the write steps to a folder containing real work.
The inside function accepts a child file or folder, not the workspace folder itself. Resolving paths can reveal a symbolic link that leads outside. There is still a limit: another program could change a path after you check it. These lessons assume a local practice folder you control. The example is not a complete protection system for a shared service where other people can change the files.
4Your practice and debugging challenge
Create your own fictional data folder with two CSV files and an unrelated text file. Print the sorted selected paths and their byte sizes without changing anything. Recursive selection looks in folders inside folders. glob("*.csv") checks one folder level; rglob("*.csv") also checks the subfolders. Decide which you need from the project instructions. Do not silently include more files than the instructions allow.
Test a missing directory, a path to the workspace itself, an outside path and a directory whose name ends in .csv. Explain why an extension match alone is insufficient. If your platform supports creating a practice symlink, verify that resolving a link outside the workspace is rejected; otherwise list it as an untested case.
5Check your understanding
Question: Why is notes.txt absent?
Answer: The written selection rule allows only CSV files in the data folder. A .txt file does not match.
Question: Why is a string-prefix check unsafe?
Answer: A folder named practice-extra begins with the letters practice but is not inside the practice folder. A path containing .. can also lead outside. Resolve the path and check its parent folders.
Question: Does selecting a path authorize editing it?
Answer: No. Finding a file and deciding to change it are separate steps.
6Save your progress
Create separate data and reports folders for your project. Keep originals in data and planned outputs in reports. Save a command that lists the allowed files, the expected list, and checks showing outside paths are rejected before any data is changed.
Next lesson: Preview a Batch Before Writing Any Reports.
Continue with the full course
This free preview is lesson 1 only. Enroll to open the rest of Python for Everyday Automation.