File Handling
Reading and Writing Data
Introduction
File handling is an essential skill in programming, allowing you to work with external data sources such as text files, CSV files, and JSON files. In this post, we’ll cover how to read from and write to files in Python, handle common errors, and manage file paths effectively.
1. Basics of File Handling
Python uses the built-in open()
function to work with files.
Opening a File
python
Copy codefile = open("example.txt", mode="r") # Open in read mode
File Modes:
r
: Read (default)w
: Write (overwrites existing content)a
: Append (adds content without overwriting)r+
: Read and write
Closing a File
Always close files to free resources:
python
Copy codefile.close()
Alternatively, use a with
statement to handle files automatically:
python
Copy codewith open("example.txt", mode="r") as file:
= file.read() content
2. Reading Files
Reading Entire Content
python
Copy codewith open("example.txt", "r") as file:
= file.read()
content print(content)
Reading Line by Line
python
Copy codewith open("example.txt", "r") as file:
for line in file:
print(line.strip()) # Remove trailing newlines
Reading Specific Number of Characters
python
Copy codewith open("example.txt", "r") as file:
= file.read(10) # Read first 10 characters
snippet print(snippet)
3. Writing Files
Writing New Content
python
Copy codewith open("example.txt", "w") as file:
file.write("This is a new line.\n")
Appending Content
python
Copy codewith open("example.txt", "a") as file:
file.write("Adding more content.\n")
4. Working with CSV Files
Python’s csv
module makes it easy to read and write CSV files.
Reading CSV Files
python
Copy codeimport csv
with open("data.csv", "r") as file:
= csv.reader(file)
reader for row in reader:
print(row)
Writing CSV Files
python
Copy codewith open("output.csv", "w", newline="") as file:
= csv.writer(file)
writer "Name", "Age", "City"])
writer.writerow(["Alice", 30, "New York"]) writer.writerow([
5. Working with JSON Files
Python’s json
module is useful for handling JSON data.
Reading JSON
python
Copy codeimport json
with open("data.json", "r") as file:
= json.load(file)
data print(data)
Writing JSON
python
Copy codewith open("output.json", "w") as file:
"name": "Alice", "age": 30}, file, indent=4) json.dump({
6. Handling Errors
When working with files, errors like missing files or permissions issues can occur. Use try-except
to handle them gracefully.
Example:
python
Copy codetry:
with open("nonexistent.txt", "r") as file:
= file.read()
content except FileNotFoundError:
print("File not found!")
except IOError:
print("An error occurred while accessing the file.")
7. Managing File Paths
Python’s os
and pathlib
modules help manage file paths across platforms.
Using os
python
Copy codeimport os
= os.path.join("folder", "file.txt")
file_path print(file_path) # Output depends on your OS
Using pathlib
python
Copy codefrom pathlib import Path
= Path("folder") / "file.txt"
file_path print(file_path)
8. Practical Example
Problem: Processing a Log File
Read a log file, filter specific entries, and save them to a new file.
Code Example:
python
Copy codewith open("log.txt", "r") as file:
= file.readlines()
lines
= [line for line in lines if "ERROR" in line]
filtered
with open("error_logs.txt", "w") as file:
file.writelines(filtered)
Conclusion
File handling in Python opens doors to working with a wide variety of data sources. Whether it’s reading plain text, parsing CSV files, or handling JSON data, mastering file operations is an indispensable skill.