Welcome to Day 13! Today we're learning a crucial skill that allows our programs to remember things: File I/O (Input/Output). File I/O is how a program reads information from a file and writes information to a file, making data persistent even after the program has finished running.
- Opening Files: You interact with files using Python's built-in
open()function. You must specify the file's name and the mode you want to open it in. - File Modes:
'r'(Read): Opens a file for reading. This is the default.'w'(Write): Opens a file for writing. Warning: This mode will overwrite any existing content in the file.'a'(Append): Opens a file for writing, but adds new content to the end of the file.
- The
withStatement: The most reliable way to handle files. Thewith open(...) as file:syntax ensures that the file is automatically closed when you're done with it, even if an error occurs. This prevents potential data corruption and memory leaks. - Reading Content:
file.read(): Reads the entire file content into a single string.file.readline(): Reads a single line from the file.file.readlines(): Reads all lines and returns them as a list of strings.
- Writing Content:
file.write("string"): Writes a string to the file. You must manually add\nfor a new line.
- To-Do List: Write a program that uses
input()to ask the user for a new to-do list item. Use'a'mode to append this item to a file namedtodo.txt. - Read and Display: Write a program that reads the entire content of a text file (e.g.,
todo.txtfrom the previous exercise) and prints it line by line using aforloop. - Create a Log File: Write a function that takes a message as an argument and appends it, along with the current time (you'll need to import the
datetimemodule for this!), to a file namedlog.txt.
- Always Use
with: Thewith open(...)statement is considered a best practice in Python. It's safer and cleaner than manually callingfile.close(). - Handle File Not Found: What if a file doesn't exist? Reading a non-existent file will cause a
FileNotFoundError. A professional program would use atry...exceptblock to gracefully handle this. (We'll cover this in a future lesson!) - File Paths: For this simple example, we're using files in the same directory. In real-world applications, you'll work with full file paths to specify locations on your computer.
- Open your terminal or command prompt.
- Navigate to the
Day_13_File_IOdirectory.cd path/to/your/fluffy-python/Day_13_File_IO - Run the script using:
python file_io_intro.py # Or if you installed python3: # python3 file_io_intro.py
Tomorrow is Day 14! We'll celebrate another week of learning with a Week 2 Recap Article, reviewing all the amazing progress we've made.