File Operations
Objective
Read, write, and manage files using Python.
Tools & Technologies
Python 3open()pathlibos
Key Commands
with open('file.txt') as f: data = f.read()pathlib.Path('dir').mkdir(parents=True)os.walk('.')json.dump(data, f)Lab Steps
01
Reading Files
Open and read text files with context managers.
02
Writing Files
Write and append to files with proper encoding specification.
03
pathlib
Use pathlib.Path for cross-platform file path manipulation.
04
JSON/CSV
Read and write JSON and CSV files with the standard library.
Challenges Encountered
- Forgetting to close files without context manager
- Encoding errors on non-UTF-8 files
Key Takeaways
- Always use with statement for file I/O — it ensures proper closing
- pathlib is more readable than os.path for path manipulation