Reading and Writing Files in Python
One of the most essential features in Python is file handling. It allows you to:
Read data from files Write data to files Process external data (logs, configs, CSV, JSON, etc.) Store output for later use
Python provides a simple and intuitive way to handle files using built-in functions.
Basic Workflow
- Open the file using
open() - Perform read or write operation
- Close the file (automatically using
with)
Opening Files
file = open("example.txt", "r") # Open for reading
file.close()Reading from a File
read() — Read Entire File
with open("example.txt", "r") as file:
content = file.read()
print(content)Explanation:
read() reads the entire file as a string.
readline() — Read One Line at a Time
with open("example.txt", "r") as file:
line1 = file.readline()
line2 = file.readline()
print(line1.strip())
print(line2.strip())Explanation:
readline() reads one line each time it is called.
Iterating Line by Line
with open("example.txt", "r") as file:
for line in file:
print(line.strip())Explanation: Looping over the file gives you each line.
Writing to a File
write() — Write Strings to File
with open("output.txt", "w") as file:
file.write("Hello, world!\n")
file.write("This is written to the file.\n")Explanation:
'w' mode overwrites the file or creates it if it doesn’t exist.
Appending to a File
a Mode — Append New Content
with open("output.txt", "a") as file:
file.write("Appending this line.\n")Explanation:
'a' mode preserves existing content and adds new data at the end.
Closing Files
When using:
file = open("example.txt", "r")
content = file.read()
file.close()👉 You must call file.close() yourself — otherwise, resources might stay open.
Using with Statement
with open("example.txt", "r") as file:
content = file.read()
print(content)Benefits: File is automatically closed Prevents resource leaks Recommended practice!
Summary
Python supports powerful, simple file I/O:
read(),readline(), iteration — for readingwrite(),append()— for writing Always usewith open()— best practice Handling files is key for most real-world applications
Next, we will cover File Modes — understanding the different ways to open files ('r', 'w', 'a', 'b', 'x', etc.).