File System & Path Operations
This section covers Python's built-in modules for working with files, directories, and filesystem operations.
📦 Modules
- pathlib - Object-oriented filesystem paths (recommended)
- os - Operating system interface and file operations
🔍 Quick Reference
| Module | Purpose | Key Classes/Functions |
|---|---|---|
pathlib | Modern path handling | Path, PurePath, WindowsPath, PosixPath |
os | OS interface | os.path, os.listdir(), os.makedirs(), os.remove() |
🚀 Common Use Cases
- Path manipulation: Join, split, and normalize file paths
- File operations: Create, read, write, and delete files
- Directory operations: List contents, create/remove directories
- Cross-platform compatibility: Handle paths across different operating systems
💡 Best Practices
- Use
pathlibfor new code - it's more readable and powerful - Use
os.pathwhen working with legacy code or when you need string paths - Always use forward slashes in code - Python handles conversion automatically