Week 6
File I/O
A file is text that lives outside the program. You open it, use it, and close it.
Open gives you a file. With closes it for you, even if something goes wrong, so you do not leave it hanging. You can read the whole file, or walk it line by line. Sorted puts lines or rows into order. CSV is a text table: commas between columns, a newline between rows. The first row is often names of columns, not data.
A path is the name of the file, including folders. The text inside the file is a different thing from that name. Start with a tiny file you wrote yourself and print exactly what you read, so you can see blank lines and spaces you did not expect.
Images are files too. Pillow opens them, can resize and crop, and can paste one image onto another using a transparent area as a mask. A fresh example: open a text file with three lines and print how many lines you got. Do not start with a photo.
Problems
- Lines of CodeRead a Python file whose name came from the command line, and count only the lines that are code.
- Pizza PyRead rows from a CSV file and print them as a table.
- ScourgifyRead a CSV, split a combined name into two columns, and write a new CSV.
- CS50 P-ShirtOpen a photo, fit it to the shirt's size, and lay the shirt on top.