Automating Files and Folders
Let a script do the clicking. You'll work with paths and folders using pathlib, find files by pattern, copy, move and rename them safely with a dry run first, turn your script into a command-line tool with options and a help page, and set it to run on a schedule. Then you'll tidy a messy Downloads folder in one command.
Updated
Files and Folders
- Paths and Folders with pathlib (Apprentice): A Path from Python's pathlib module holds a file's or folder's address in parts, so a script can join paths with /, read a file's name and extension, list and find the files in a folder, and make folders, the same way on Windows and on a Mac.
- Copying, Moving and Renaming Files Safely (Journeyman): shutil.copy2 copies a file, shutil.move and Path.rename move and rename one, and three habits keep a file-moving script safe, a backup copy, a check that the destination is free, and a dry run that shows the plan before anything moves.
From Script to Tool
- Command-Line Options with argparse (Journeyman): The words typed after a script's name arrive in sys.argv, and the argparse module turns them into a positional argument, options with defaults and flags, checks them, and writes a --help page, so one script works on any folder without editing its code.
- Running a Script on a Schedule (Journeyman): Windows Task Scheduler and cron on a Mac run a script at set times with no terminal open, so the job names the virtual environment's python and the script by their full paths, the script finds its files from its own folder, and logging records each run in a log file.
Exercise: Downloads Sorter
- Exercise: Downloads Sorter (Journeyman): Finish a command-line tool that sorts a folder's files into Photos, Documents, Installers and Archives, planning the moves first, showing them with --dry-run, and never replacing or deleting a file, while pytest checks each step on folders it builds for itself.