Suppose an experiment produces several files per participant. How do you gather only the data files and remove unwanted text files without losing track of what changed? A file operation is a programmatic action on a file, such as reading it, moving it, copying it, or checking when it was modified. Writing code for repeated manipulations makes the work more reproducible and trackable than doing it manually. section 11.0 section 11.4 section 11.4.6
subject_123.config subject_123.dat subject_123.txt subject_124.config subject_124.dat subject_124.txt subject_125.config subject_125.dat subject_125.txt
Each participant has .config, .dat, and .txt files. The task is to select the .dat files, move them to a new directory, and delete the .txt files. section 11.4
Read a file before processing its contents
For content-based work, start with a context manager: with open("tasks.txt") as file: data = file.read() print(data) If tasks.txt contains `1001,Homework,5`, that line is printed. The `with` statement closes the file when its body ends, avoiding a separate `close()` call. section 11.1.1
Keep the two kinds of work distinct: reading and writing handle file contents; selecting, moving, copying, and deleting handle files themselves. Python offers more than one approach to file management, including os and pathlib. Choose a clear approach and record repeated operations in code. section 11.0 section 11.4 section 11.4.6
