6 of 20 topics completed
Python File I/O: Working with Files
Working with files is a crucial skill for any programmer. Whether you're reading data from a configuration file, saving user information, or analyzing large datasets, understanding file I/O (Input/Output) operations in Python will help you create more powerful programs.
💡 Real-World Applications
File I/O is used in countless real-world scenarios:
- Reading configuration settings
- Storing user data and preferences
- Processing log files
- Importing and exporting data (CSV, JSON, etc.)
- Saving game progress
File Operations Overview

Working with files in Python generally follows this pattern:
- Open the file
- Read from or write to the file
- Close the file
Opening Files
To work with a file, you first need to open it using the open()
function:
1# Basic file opening syntax2file = open("filename.txt", "mode")
The mode
parameter determines what operations you can perform:
Mode | Description | Creates File? | Overwrites? |
---|---|---|---|
"r" | Read (default) | No | No |
"w" | Write | Yes | Yes |
"a" | Append | Yes | No |
"r+" | Read and write | No | No |
"b" | Binary mode (add to other modes) | - | - |
⚠️ Important Note
Always close your files after you're done with them to free up system resources. The best way to ensure this happens is using the with
statement, which we'll cover below.
Reading from Files
There are several ways to read data from a file:
1. Reading the Entire File
1# Reading the entire file content at once2with open("sample.txt", "r") as file:3 content = file.read()4 print(content)56# The 'with' statement automatically closes the file when the block ends
2. Reading Line by Line
1# Reading file line by line2with open("sample.txt", "r") as file:3 for line in file:4 # The line variable will contain the newline character at the end5 print(line, end="") # Using end="" to avoid double newlines67 # Or you can strip the newline character8 # print(line.strip())
3. Reading All Lines into a List
1# Reading all lines into a list2with open("sample.txt", "r") as file:3 lines = file.readlines()45 # Now 'lines' is a list where each element is a line from the file6 for line in lines:7 print(line.strip())89 # You can also access specific lines by index10 if len(lines) > 0:11 print("First line:", lines[0].strip())
Writing to Files
Writing data to files is just as straightforward:
1. Writing Text
1# Writing to a file (creates or overwrites the file)2with open("output.txt", "w") as file:3 file.write("Hello, World!\n")4 file.write("This is a second line.\n")56 # You can also write multiple lines at once7 lines = ["Line 1\n", "Line 2\n", "Line 3\n"]8 file.writelines(lines)
2. Appending to a File
1# Appending to a file (adds to the end without overwriting)2with open("log.txt", "a") as file:3 file.write("New log entry: User logged in\n")45# This is useful for adding data without losing existing content
The "with" Statement
Using with
for file operations has two major advantages:
- It automatically closes the file when the block ends
- It handles exceptions properly
This is the recommended way to work with files in Python.
Without "with" Statement
If you don't use with
, you need to manually close files:
# Old method (not recommended)file = open("data.txt", "r")try:content = file.read()print(content)finally:file.close() # Must remember to close!
Working with CSV Files
CSV (Comma-Separated Values) files are commonly used for storing tabular data:
1import csv23# Writing data to a CSV file4with open("students.csv", "w", newline="") as file:5 writer = csv.writer(file)67 # Write the header row8 writer.writerow(["Name", "Age", "Grade"])910 # Write data rows11 writer.writerow(["Alice", 15, 95.5])12 writer.writerow(["Bob", 16, 89.0])13 writer.writerow(["Charlie", 15, 92.3])1415# Reading data from a CSV file16with open("students.csv", "r") as file:17 reader = csv.reader(file)1819 # Skip the header row20 header = next(reader)21 print(f"Columns: {header}")2223 # Process each row24 for row in reader:25 name, age, grade = row26 print(f"{name} is {age} years old with grade {grade}")
Working with JSON Files
JSON (JavaScript Object Notation) is a popular format for storing structured data:
1import json23# Data to be written4user = {5 "name": "John Smith",6 "age": 30,7 "is_student": False,8 "courses": ["Python", "Data Science", "Web Development"],9 "address": {10 "street": "123 Main St",11 "city": "Codeville",12 "zipcode": "12345"13 }14}1516# Writing JSON to a file17with open("user_data.json", "w") as file:18 json.dump(user, file, indent=4) # indent for pretty formatting1920# Reading JSON from a file21with open("user_data.json", "r") as file:22 loaded_user = json.load(file)2324 print(f"Name: {loaded_user['name']}")25 print(f"Age: {loaded_user['age']}")26 print(f"Courses: {', '.join(loaded_user['courses'])}")27 print(f"City: {loaded_user['address']['city']}")
File and Directory Operations
Python's os
module provides functions for working with files and directories:
1import os23# Check if a file exists4if os.path.exists("config.txt"):5 print("Config file found!")6else:7 print("Config file not found.")89# Get file information10if os.path.isfile("data.txt"):11 size = os.path.getsize("data.txt")12 print(f"File size: {size} bytes")1314 modified_time = os.path.getmtime("data.txt")15 print(f"Last modified: {modified_time}")1617# List files in a directory18files = os.listdir(".") # "." means current directory19print("Files in current directory:")20for file in files:21 print(f" - {file}")2223# Create a new directory24os.makedirs("output", exist_ok=True) # exist_ok=True prevents errors if dir exists2526# Delete a file27if os.path.exists("temp.txt"):28 os.remove("temp.txt")29 print("Temporary file deleted.")
Practical Example: Simple Note-Taking App
Let's create a simple command-line note-taking application that uses file I/O:
1def note_app():2 """A simple note-taking application."""3 notes_file = "my_notes.txt"45 # Create the file if it doesn't exist6 if not os.path.exists(notes_file):7 with open(notes_file, "w") as f:8 f.write("# My Notes\n\n")910 while True:11 print("\n===== Note Taking App =====")12 print("1. View all notes")13 print("2. Add a new note")14 print("3. Clear all notes")15 print("4. Exit")1617 choice = input("Enter your choice (1-4): ")1819 if choice == "1":20 # View all notes21 with open(notes_file, "r") as f:22 content = f.read()23 if content.strip() == "# My Notes":24 print("\nNo notes yet!")25 else:26 print("\n" + content)2728 elif choice == "2":29 # Add a new note30 note = input("\nEnter your note: ")31 if note:32 with open(notes_file, "a") as f:33 from datetime import datetime34 timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")35 f.write(f"[{timestamp}] {note}\n")36 print("Note saved successfully!")3738 elif choice == "3":39 # Clear all notes40 confirm = input("\nAre you sure you want to clear all notes? (y/n): ")41 if confirm.lower() == 'y':42 with open(notes_file, "w") as f:43 f.write("# My Notes\n\n")44 print("All notes cleared!")4546 elif choice == "4":47 # Exit the app48 print("\nThank you for using Note Taking App!")49 break5051 else:52 print("\nInvalid choice. Please try again.")5354# To run the app: note_app()
🎯 Try it yourself!
Enhance the note-taking app with these additional features:
- Search for notes containing specific text
- Delete individual notes
- Categorize notes with tags
- Export notes to CSV or JSON format
Error Handling with File Operations
When working with files, many things can go wrong (file not found, permission errors, etc.). It's important to handle these errors gracefully:
1# Example of error handling with file operations2try:3 with open("config.txt", "r") as file:4 config = file.read()5 print("Configuration loaded successfully.")6except FileNotFoundError:7 print("Configuration file not found. Using default settings.")8except PermissionError:9 print("No permission to read the configuration file.")10except Exception as e:11 print(f"An error occurred: {e}")12finally:13 print("Configuration process completed.")
Best Practices for File I/O
- Always use the
with
statement to ensure files are properly closed. - Handle exceptions to make your program robust against file errors.
- Check if files exist before trying to read from them.
- Use appropriate modules for specific file formats (csv, json, etc.).
- Be careful with write mode (
"w"
) as it overwrites existing files. - Use relative paths when possible to make your code more portable.
Summary
In this tutorial, you've learned:
- How to open, read from, and write to files
- Different file modes (read, write, append)
- Working with CSV and JSON files
- Basic file and directory operations
- Error handling for file operations
- Best practices for file I/O
File I/O operations are essential for building applications that can store and retrieve data, making your programs more useful and powerful.
Related Tutorials
Learn to interact with users and display information.
Learn moreMaster string manipulation in Python.
Learn moreLearn how to handle errors and exceptions in Python.
Learn more