In the previous post, we introduced modules and libraries, enabling you to incorporate external functionality into your programs. Now, let’s turn our attention to a fundamental aspect of many applications: handling data stored in files. In this post, we’ll cover:
- Reading from and writing to text files
- Understanding file paths and file modes
- Simple data processing tasks
By the end, you’ll know how to save your program’s output to a file, read data into your code, and perform basic data transformations.
Why Work With Files?
While printing to the screen or storing data in variables is fine for small tasks, most real-world programs need to save and retrieve information. Whether it’s configuration files, user-generated content, logs, or large datasets, working with files lets you store data between program runs and share information easily.
Opening and Closing Files
To work with files in Python, you use the built-in open() function, which gives you a file object. You can specify:
- The file path (like data.txt if it’s in the same directory, or a full path like /home/user/data.txt).
- The mode, which indicates what you plan to do with the file:
- "r": Read from the file (default).
- "w": Write to the file (overwrites existing content).
- "a": Append to the end of the file.
- "r+": Read and write.
Once you’re done with the file, it’s good practice to close it, which releases system resources.
Example of reading a file:
file = open("data.txt", "r") # Open file in read mode
content = file.read()
file.close()
print(content)
Check out this video on reading files for a quick intro.
Using the with Statement
To avoid forgetting to close your files, Python provides the with statement, which handles closing automatically:
with open("data.txt", "r") as file:
content = file.read()
print(content)
When the with block finishes, file is closed automatically, even if errors occur.
Reading Files Line by Line
If you have large files, reading them all at once may not be efficient. Instead, you can read line by line:
with open("data.txt", "r") as file:
for line in file:
print(line.strip()) # .strip() removes trailing newline characters
You can also use file.readline() or file.readlines() if you prefer a more manual approach.
Writing to Files
To create or overwrite a file, open it in write mode:
with open("output.txt", "w") as file:
file.write("Hello, world!\n")
file.write("This is a new file.")
If output.txt doesn’t exist, it will be created. If it does exist, its content will be replaced.
To append to a file without overwriting its existing content, use "a":
with open("output.txt", "a") as file:
file.write("\nAdding another line at the end.")
Understanding File Paths
- Relative paths: Refer to files relative to the current working directory (e.g., data.txt or folder/data.txt).
- Absolute paths: Refer to files from the root of the system (e.g., C:\Users\Alice\data.txt on Windows or /home/alice/data.txt on Linux/macOS).
You can use the os module to work with file paths in a platform-independent way:
import os
current_dir = os.getcwd()
file_path = os.path.join(current_dir, "data.txt")
with open(file_path, "r") as file:
print(file.read())
Simple Data Processing Tasks
Once you have data in your program, you can process it however you like. For example, if you have a file of numbers (one per line), you could calculate their sum:
total = 0
with open("numbers.txt", "r") as file:
for line in file:
number = int(line.strip())
total += number
print("Total:", total)
For CSV files, you can split lines by a delimiter, or you might use Python’s built-in csv module to handle more complex parsing. As you progress, you’ll likely work with data formats like JSON or XML. Python’s json module can handle JSON data easily:
import json
with open("data.json", "r") as file:
data = json.load(file)
print("Data loaded:", data)
Small Project: Simple Contact List
Imagine you have a file called contacts.txt that stores names and phone numbers in this format:
Alice,123-456-7890
Bob,987-654-3210
Charlie,555-555-5555
You can write a program to read this data, store it in a dictionary, and then allow the user to look up phone numbers:
contacts = {}
# Load contacts from file
with open("contacts.txt", "r") as file:
for line in file:
name, phone = line.strip().split(",")
contacts[name] = phone
# Ask user for a name and print the phone number
search_name = input("Enter a name to look up: ")
if search_name in contacts:
print(search_name, "->", contacts[search_name])
else:
print("No contact found for", search_name)
This example demonstrates reading from a file, parsing the data, and using it at runtime.
What’s Next?
You now know how to read and write files, handle file paths, and process basic data. File operations enable your programs to interact with the real world more tangibly, storing results and consuming existing information.
In the next post, we’ll introduce Object-Oriented Programming (OOP). OOP helps you structure your code around objects—bundling data and functions together—which can make larger, more complex programs easier to manage.
Coming Up: Getting Started with Python for Beginners #8: Introduction to Object-Oriented Programming
- Understanding classes and objects
- Defining attributes and methods
- Creating your own classes to model real-world concepts
If you’re curious, you can start learning about OOP concepts now by watching this introduction to OOP in Python.
Wrapping Up
File operations unlock a whole new world for your Python projects. Whether you’re logging results, reading configurations, or processing datasets, handling files is a crucial skill. Keep experimenting—try reading different file formats, writing output in various ways, and integrating file I/O into your previous projects.