File Handling in Python: Reading and Writing Data
Master Python File Handling. Learn how to open, read, and write TXT and CSV files directly using Python for Machine Learning data preparation.
Introduction
Artificial Intelligence algorithms are hungry for data. That data doesn't magically appear in your Python variables; it lives on your hard drive in text files, CSVs, and JSON files. File Handling is the process of using Python to connect to these files, extract their text, process it, and save the results back to the hard drive.
What You Will Learn
- How to open and read text files using Python.
- How to write and append data to files.
- The critical
with open()context manager. - How File Handling scales into Data Science.
Why This Topic Matters
The very first line of code in 99% of Data Science projects involves opening a file (usually a .csv file). While advanced libraries like Pandas handle the heavy lifting today, understanding native Python file handling is critical for reading custom logs, writing configuration files, or handling unstructured text datasets.
Prerequisites
Detailed Explanation & Examples
To work with files in Python, we use the built-in open() function. It takes two primary arguments: the file path, and the Mode.
File Modes:
"r"- Read (Default mode. Opens a file for reading. Errors if file does not exist)."w"- Write (Opens a file for writing. Overwrites the file if it exists. Creates it if it doesn't)."a"- Append (Opens a file for appending text to the end. Creates it if it doesn't exist).
1. The Traditional (but dangerous) Way
# 1. Open the file
file = open("training_data.txt", "w")
# 2. Write to the file
file.write("Epoch 1: Loss 0.5")
# 3. CLOSE THE FILE! (Crucial)
file.close()
Why is this dangerous? If your code crashes between open() and close(), the file remains "locked" in memory and can become corrupted.
2. The Pythonic Way (The with statement)
Professional developers exclusively use the with statement (a context manager). It guarantees that the file will be safely closed automatically, even if the program crashes.
Writing to a file:
# Creates 'metrics.txt' and writes to it
with open("metrics.txt", "w") as file:
file.write("Accuracy: 95%\n")
file.write("Loss: 0.02\n")
# The file is automatically closed right here!
Appending to a file:
# 'a' adds to the end of the file instead of deleting what is there
with open("metrics.txt", "a") as file:
file.write("Training Complete.\n")
Reading from a file:
# 'r' reads the data into a Python string variable
try:
with open("metrics.txt", "r") as file:
data = file.read()
print("File Contents:\n" + data)
except FileNotFoundError:
print("Error: That file does not exist!")
Reading Datasets (Lines)
Often, you don't want to read a massive 10GB file into memory all at once. You want to process it line by line.
with open("massive_dataset.csv", "r") as file:
for line in file:
# Process one row of data at a time
clean_line = line.strip() # Removes the invisible \n newline character
# print(clean_line)
Visual Diagram (Mermaid)
graph LR
A[Hard Drive: data.csv] -->|with open('r')| B(Python Variable)
B -->|Machine Learning Magic| C(Python Prediction)
C -->|with open('w')| D[Hard Drive: output.txt]
style A fill:#475569,stroke:#fff,color:#fff
style D fill:#10B981,stroke:#fff,color:#fff
Industry Use Cases
- Logging: When training an AI for 5 days, engineers use file handling in "append" mode (
"a") to constantly write the AI's current accuracy to a.logfile every hour. If the system crashes on day 3, they have a written record of its progress up to that hour. - NLP Document Processing: Reading 100,000 PDF and text documents from a folder into Python strings to train a custom GPT model.
Advantages
- Memory Efficiency: Iterating over a file line-by-line using a
forloop means Python only loads one line into RAM at a time. This allows you to process datasets that are larger than your computer's RAM.
Best Practices
- Always use
with open(): Never manually usefile.open()andfile.close(). - Handle Encodings: When reading text files downloaded from the internet, always specify the encoding to prevent weird character crashes:
with open("data.txt", "r", encoding="utf-8").
Common Mistakes
- Opening in the wrong mode: Opening a file with
"w"instead of"a"will instantly and permanently erase everything that was previously in the file. - FileNotFoundError: Forgetting that file paths are relative to where you run the script from, not where the script is located.
Summary
File Handling allows your Python scripts to interact with the permanent storage of your computer. By using the with open() context manager, you can safely read massive datasets into memory for your AI models, and write the model's predictions back out to text or CSV files.
Next Topic
Native Python lists and math operators are too slow for real AI. It's time to upgrade our tools. Move on to the most important math library in Python: NumPy.