Progress0%

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

File I/O Workflow

Working with files in Python generally follows this pattern:

  1. Open the file
  2. Read from or write to the file
  3. Close the file

Opening Files

To work with a file, you first need to open it using the open() function:

Python
1# Basic file opening syntax
2file = open("filename.txt", "mode")

The mode parameter determines what operations you can perform:

ModeDescriptionCreates File?Overwrites?
"r"Read (default)NoNo
"w"WriteYesYes
"a"AppendYesNo
"r+"Read and writeNoNo
"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

Python
1# Reading the entire file content at once
2with open("sample.txt", "r") as file:
3 content = file.read()
4 print(content)
5
6# The 'with' statement automatically closes the file when the block ends

2. Reading Line by Line

Python
1# Reading file line by line
2with open("sample.txt", "r") as file:
3 for line in file:
4 # The line variable will contain the newline character at the end
5 print(line, end="") # Using end="" to avoid double newlines
6
7 # Or you can strip the newline character
8 # print(line.strip())

3. Reading All Lines into a List

Python
1# Reading all lines into a list
2with open("sample.txt", "r") as file:
3 lines = file.readlines()
4
5 # Now 'lines' is a list where each element is a line from the file
6 for line in lines:
7 print(line.strip())
8
9 # You can also access specific lines by index
10 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

Python
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")
5
6 # You can also write multiple lines at once
7 lines = ["Line 1\n", "Line 2\n", "Line 3\n"]
8 file.writelines(lines)

2. Appending to a File

Python
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")
4
5# This is useful for adding data without losing existing content

The "with" Statement

Using with for file operations has two major advantages:

  1. It automatically closes the file when the block ends
  2. 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:

Python
# 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:

Python
1import csv
2
3# Writing data to a CSV file
4with open("students.csv", "w", newline="") as file:
5 writer = csv.writer(file)
6
7 # Write the header row
8 writer.writerow(["Name", "Age", "Grade"])
9
10 # Write data rows
11 writer.writerow(["Alice", 15, 95.5])
12 writer.writerow(["Bob", 16, 89.0])
13 writer.writerow(["Charlie", 15, 92.3])
14
15# Reading data from a CSV file
16with open("students.csv", "r") as file:
17 reader = csv.reader(file)
18
19 # Skip the header row
20 header = next(reader)
21 print(f"Columns: {header}")
22
23 # Process each row
24 for row in reader:
25 name, age, grade = row
26 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:

Python
1import json
2
3# Data to be written
4user = {
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}
15
16# Writing JSON to a file
17with open("user_data.json", "w") as file:
18 json.dump(user, file, indent=4) # indent for pretty formatting
19
20# Reading JSON from a file
21with open("user_data.json", "r") as file:
22 loaded_user = json.load(file)
23
24 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:

Python
1import os
2
3# Check if a file exists
4if os.path.exists("config.txt"):
5 print("Config file found!")
6else:
7 print("Config file not found.")
8
9# Get file information
10if os.path.isfile("data.txt"):
11 size = os.path.getsize("data.txt")
12 print(f"File size: {size} bytes")
13
14 modified_time = os.path.getmtime("data.txt")
15 print(f"Last modified: {modified_time}")
16
17# List files in a directory
18files = os.listdir(".") # "." means current directory
19print("Files in current directory:")
20for file in files:
21 print(f" - {file}")
22
23# Create a new directory
24os.makedirs("output", exist_ok=True) # exist_ok=True prevents errors if dir exists
25
26# Delete a file
27if 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:

Python
1def note_app():
2 """A simple note-taking application."""
3 notes_file = "my_notes.txt"
4
5 # Create the file if it doesn't exist
6 if not os.path.exists(notes_file):
7 with open(notes_file, "w") as f:
8 f.write("# My Notes\n\n")
9
10 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")
16
17 choice = input("Enter your choice (1-4): ")
18
19 if choice == "1":
20 # View all notes
21 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)
27
28 elif choice == "2":
29 # Add a new note
30 note = input("\nEnter your note: ")
31 if note:
32 with open(notes_file, "a") as f:
33 from datetime import datetime
34 timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
35 f.write(f"[{timestamp}] {note}\n")
36 print("Note saved successfully!")
37
38 elif choice == "3":
39 # Clear all notes
40 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!")
45
46 elif choice == "4":
47 # Exit the app
48 print("\nThank you for using Note Taking App!")
49 break
50
51 else:
52 print("\nInvalid choice. Please try again.")
53
54# 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:

Python
1# Example of error handling with file operations
2try:
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

  1. Always use the with statement to ensure files are properly closed.
  2. Handle exceptions to make your program robust against file errors.
  3. Check if files exist before trying to read from them.
  4. Use appropriate modules for specific file formats (csv, json, etc.).
  5. Be careful with write mode ("w") as it overwrites existing files.
  6. 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 more

Master string manipulation in Python.

Learn more

Learn how to handle errors and exceptions in Python.

Learn more