Block 131: Filesystem Automation: os & pathlib
Automate file and directory operations from Python.
Concepts
- from pathlib import Path
- Path().glob() and Path().rglob()
- shutil.copy(), shutil.move(), shutil.rmtree()
- os.makedirs(), os.listdir(), os.path.exists()
Code Examples
See exercise below.
Exercise
Write a script that organizes files in a folder into subfolders by extension (.pdf/, .csv/, .jpg/ etc.). List all .csv files in a directory tree recursively with pathlib.
Homework
Why is pathlib preferred over os.path for new code? List 3 practical advantages.