# Read entire file at once
with open('data/sample.txt', 'r') as file:
content = file.read()
print(content)Hello, World!
This is a sample text file.
It contains multiple lines.
Python makes file handling easy.
Learning Python is fun!
Working with files is a fundamental skill in Python programming. Whether you’re reading data, saving results, or organizing files, understanding file operations is essential. In this tutorial, you’ll learn:
open()globBasic Python knowledge (variables, loops, functions) is recommended.
open()The open() function is your gateway to reading files in Python. The most common way to read a file is using a context manager with the with statement.
# Read entire file at once
with open('data/sample.txt', 'r') as file:
content = file.read()
print(content)Hello, World!
This is a sample text file.
It contains multiple lines.
Python makes file handling easy.
Learning Python is fun!
Key Points: - 'r' means “read mode” - with automatically closes the file when done - read() returns the entire file as a string
# Method 1: Using readlines()
with open('data/sample.txt', 'r') as file:
lines = file.readlines()
print("Number of lines:", len(lines))
print("First line:", lines[0])Number of lines: 5
First line: Hello, World!
# Method 2: Iterating directly (memory efficient)
with open('data/sample.txt', 'r') as file:
for line_num, line in enumerate(file, 1):
print(f"Line {line_num}: {line.strip()}")Line 1: Hello, World!
Line 2: This is a sample text file.
Line 3: It contains multiple lines.
Line 4: Python makes file handling easy.
Line 5: Learning Python is fun!
The .strip() method removes whitespace and newline characters from both ends of a string.
# Read only first 3 lines
with open('data/shopping_list.txt', 'r') as file:
for i in range(3):
line = file.readline()
print(f"Item {i+1}: {line.strip()}")Item 1: Apples
Item 2: Bananas
Item 3: Milk
# Write mode ('w') - overwrites existing content!
with open('data/output.txt', 'w') as file:
file.write("This is the first line\n")
file.write("This is the second line\n")
# Verify what we wrote
with open('data/output.txt', 'r') as file:
print(file.read())This is the first line
This is the second line
# Append mode ('a') - adds to the end
with open('data/output.txt', 'a') as file:
file.write("This line was appended\n")
# Check the updated content
with open('data/output.txt', 'r') as file:
print(file.read())This is the first line
This is the second line
This line was appended
# Using writelines()
lines_to_write = ["Apple\n", "Banana\n", "Cherry\n"]
with open('data/fruits.txt', 'w') as file:
file.writelines(lines_to_write)
# Read it back
with open('data/fruits.txt', 'r') as file:
print(file.read())Apple
Banana
Cherry
| Mode | Description | Creates New? | Truncates? |
|---|---|---|---|
'r' |
Read only | No | No |
'w' |
Write only | Yes | Yes |
'a' |
Append only | Yes | No |
'r+' |
Read and write | No | No |
'w+' |
Read and write | Yes | Yes |
# Read shopping list and count items
with open('data/shopping_list.txt', 'r') as file:
items = [line.strip() for line in file if line.strip()]
print(f"You have {len(items)} items to buy:")
for idx, item in enumerate(items, 1):
print(f"{idx}. {item}")
# Add more items
new_items = ["Pasta", "Olive Oil"]
with open('data/shopping_list.txt', 'a') as file:
for item in new_items:
file.write(f"{item}\n")
print("\nUpdated list:")
with open('data/shopping_list.txt', 'r') as file:
print(file.read())You have 15 items to buy:
1. Apples
2. Bananas
3. Milk
4. Bread
5. Eggs
6. Cheese
7. Orange Juice
8. Avocados
9. Almonds
10. Pasta
11. Olive Oil
12. Pasta
13. Olive Oil
14. Pasta
15. Olive Oil
Updated list:
Apples
Bananas
Milk
Bread
Eggs
Cheese
Orange Juice
Avocados
Almonds
Pasta
Olive Oil
Pasta
Olive Oil
Pasta
Olive Oil
Pasta
Olive Oil
JSON (JavaScript Object Notation) is a popular format for storing and exchanging structured data. Python’s json module makes it easy to work with.
import json
# Load JSON from a file
with open('data/students.json', 'r') as file:
data = json.load(file)
print("Type:", type(data))
print("\nJSON Data:")
print(json.dumps(data, indent=2))Type: <class 'dict'>
JSON Data:
{
"students": [
{
"name": "Alice",
"age": 20,
"grade": 85,
"courses": [
"Math",
"Physics"
]
},
{
"name": "Bob",
"age": 22,
"grade": 92,
"courses": [
"Chemistry",
"Biology"
]
},
{
"name": "Charlie",
"age": 21,
"grade": 78,
"courses": [
"Math",
"Computer Science"
]
},
{
"name": "Diana",
"age": 23,
"grade": 95,
"courses": [
"Physics",
"Engineering"
]
},
{
"name": "Eve",
"age": 20,
"grade": 88,
"courses": [
"Biology",
"Chemistry"
]
}
],
"school": "Data Science Academy",
"year": 2024
}
# Access the students list
students = data['students']
print(f"Total students: {len(students)}\n")
# Loop through students
for student in students:
print(f"{student['name']} - Grade: {student['grade']}, Courses: {student['courses']}")Total students: 5
Alice - Grade: 85, Courses: ['Math', 'Physics']
Bob - Grade: 92, Courses: ['Chemistry', 'Biology']
Charlie - Grade: 78, Courses: ['Math', 'Computer Science']
Diana - Grade: 95, Courses: ['Physics', 'Engineering']
Eve - Grade: 88, Courses: ['Biology', 'Chemistry']
# Add a new student
new_student = {
"name": "David Brown",
"age": 22,
"grade": 90,
"courses": ["Engineering", "Math"]
}
data['students'].append(new_student)
# Save back to file
with open('data/students_updated.json', 'w') as file:
json.dump(data, file, indent=2)
print("Updated data saved!")
# Verify
with open('data/students_updated.json', 'r') as file:
updated_data = json.load(file)
print(f"\nNow we have {len(updated_data['students'])} students")Updated data saved!
Now we have 6 students
# Load inventory data
with open('data/inventory.json', 'r') as file:
inventory = json.load(file)
print(f"Warehouse: {inventory['warehouse']}")
print(f"Last Updated: {inventory['last_updated']}\n")
print("Products:")
for product in inventory['products']:
print(f" {product['name']}: ${product['price']} (Qty: {product['quantity']})")Warehouse: Main
Last Updated: 2024-01-15
Products:
Laptop: $999.99 (Qty: 50)
Mouse: $29.99 (Qty: 200)
Keyboard: $79.99 (Qty: 150)
Monitor: $399.99 (Qty: 75)
Headphones: $149.99 (Qty: 100)
# json.loads() - load from string
json_string = '{"name": "Eve", "age": 25, "city": "Boston"}'
person = json.loads(json_string)
print("From string:", person)
# json.dumps() - convert to string
person['occupation'] = 'Data Scientist'
json_output = json.dumps(person, indent=2)
print("\nTo string:")
print(json_output)From string: {'name': 'Eve', 'age': 25, 'city': 'Boston'}
To string:
{
"name": "Eve",
"age": 25,
"city": "Boston",
"occupation": "Data Scientist"
}
Remember: - json.load() - read from file - json.loads() - read from string - json.dump() - write to file - json.dumps() - write to string
globThe glob module helps you find files using pattern matching, similar to how you search in your file system.
import glob
# Find all .txt files in data directory
txt_files = glob.glob('data/*.txt')
print("Text files found:")
for file in txt_files:
print(f" - {file}")Text files found:
- data/shopping_list.txt
- data/config.txt
- data/fruits.txt
- data/employees.txt
- data/output.txt
- data/sample.txt
| Pattern | Matches |
|---|---|
* |
Any characters |
? |
Single character |
[abc] |
Any of a, b, or c |
[!abc] |
Not a, b, or c |
** |
Any subdirectory (recursive) |
# Find all JSON files
json_files = glob.glob('data/*.json')
print(f"Found {len(json_files)} JSON files:")
for file in json_files:
print(f" - {file}")Found 4 JSON files:
- data/students_updated.json
- data/products.json
- data/inventory.json
- data/students.json
# Find all log files
log_files = glob.glob('data/log_*.txt')
print("Log files:")
for file in sorted(log_files):
print(f" - {file}")Log files:
# Find all report files
report_files = glob.glob('data/report_*.txt')
print("\nReport files:")
for file in sorted(report_files):
print(f" - {file}")
Report files:
# Find all .txt files in current directory and subdirectories
all_txt = glob.glob('**/*.txt', recursive=True)
print(f"Total .txt files (including subdirectories): {len(all_txt)}")Total .txt files (including subdirectories): 7
import os
# Process all log files
log_files = glob.glob('data/log_*.txt')
print("Log File Analysis")
print("=" * 50)
for log_file in sorted(log_files):
file_size = os.path.getsize(log_file)
with open(log_file, 'r') as file:
lines = file.readlines()
print(f"\nFile: {os.path.basename(log_file)}")
print(f" Size: {file_size} bytes")
print(f" Lines: {len(lines)}")
print(f" First entry: {lines[0].strip()}")Log File Analysis
==================================================
# Process all JSON files
json_files = glob.glob('data/*.json')
for json_file in json_files:
with open(json_file, 'r') as file:
data = json.load(file)
print(f"\n{os.path.basename(json_file)}:")
print(f" Top-level keys: {list(data.keys())}")
students_updated.json:
Top-level keys: ['students', 'school', 'year']
products.json:
Top-level keys: ['products']
inventory.json:
Top-level keys: ['products', 'warehouse', 'last_updated']
students.json:
Top-level keys: ['students', 'school', 'year']
Now it’s time to practice! Try these exercises to reinforce your learning.
Task: Create a Python script that: 1. Reads data/shopping_list.txt 2. Counts how many items contain the letter ‘a’ 3. Writes those items to a new file exercises/items_with_a.txt
# Your code here
# with open('data/shopping_list.txt', 'r') as file:
# ...Solution:
# Solution
with open('data/shopping_list.txt', 'r') as file:
items = [line.strip() for line in file if line.strip()]
items_with_a = [item for item in items if 'a' in item.lower()]
with open('exercises/items_with_a.txt', 'w') as file:
for item in items_with_a:
file.write(f"{item}\n")
print(f"Found {len(items_with_a)} items with 'a':")
for item in items_with_a:
print(f" - {item}")Found 10 items with 'a':
- Apples
- Bananas
- Bread
- Orange Juice
- Avocados
- Almonds
- Pasta
- Pasta
- Pasta
- Pasta
Task: 1. Load data/students.json 2. Find students with GPA > 3.7 3. Save only those students to exercises/high_achievers.json
# Your code hereSolution:
# Solution
import json
with open('data/students.json', 'r') as file:
data = json.load(file)
high_achievers = [s for s in data['students'] if s['grade'] > 85]
output = {"high_achievers": high_achievers}
with open('exercises/high_achievers.json', 'w') as file:
json.dump(output, file, indent=2)
print(f"Found {len(high_achievers)} high achievers:")
for student in high_achievers:
print(f" {student['name']}: {student['grade']}")Found 3 high achievers:
Bob: 92
Diana: 95
Eve: 88
Task: 1. Use glob to find all files starting with “log_” in the data directory 2. For each file, count the number of lines 3. Print a summary report
# Your code hereSolution:
# Solution
import glob
log_files = glob.glob('data/log_*.txt')
print("Log File Summary")
print("-" * 40)
total_lines = 0
for log_file in sorted(log_files):
with open(log_file, 'r') as file:
line_count = len(file.readlines())
total_lines += line_count
print(f"{os.path.basename(log_file)}: {line_count} lines")
print("-" * 40)
print(f"Total lines across all log files: {total_lines}")Log File Summary
----------------------------------------
----------------------------------------
Total lines across all log files: 0
Task: Create a file inventory system that: 1. Uses glob to find all files in the data/ directory 2. Creates a JSON file with information about each file: - filename - extension - size in bytes - number of lines (for text files) 3. Saves this inventory to exercises/file_inventory.json
# Your code hereSolution:
# Solution
import glob
import os
import json
files = glob.glob('data/*')
inventory = []
for filepath in files:
if os.path.isfile(filepath):
filename = os.path.basename(filepath)
extension = os.path.splitext(filename)[1]
size = os.path.getsize(filepath)
file_info = {
"filename": filename,
"extension": extension,
"size_bytes": size
}
# Count lines for text files
if extension in ['.txt', '.json']:
try:
with open(filepath, 'r') as file:
line_count = len(file.readlines())
file_info["lines"] = line_count
except:
file_info["lines"] = "N/A"
inventory.append(file_info)
# Save inventory
output = {"inventory": inventory, "total_files": len(inventory)}
with open('exercises/file_inventory.json', 'w') as file:
json.dump(output, file, indent=2)
print(f"Inventory created with {len(inventory)} files")
print(json.dumps(output, indent=2))Inventory created with 12 files
{
"inventory": [
{
"filename": "shopping_list.txt",
"extension": ".txt",
"size_bytes": 132,
"lines": 17
},
{
"filename": "config.txt",
"extension": ".txt",
"size_bytes": 99,
"lines": 6
},
{
"filename": "students_updated.json",
"extension": ".json",
"size_bytes": 906,
"lines": 60
},
{
"filename": "fruits.txt",
"extension": ".txt",
"size_bytes": 20,
"lines": 3
},
{
"filename": "sample_data.csv",
"extension": ".csv",
"size_bytes": 129
},
{
"filename": "employees.txt",
"extension": ".txt",
"size_bytes": 128,
"lines": 5
},
{
"filename": "products.json",
"extension": ".json",
"size_bytes": 507,
"lines": 9
},
{
"filename": "inventory.json",
"extension": ".json",
"size_bytes": 443,
"lines": 11
},
{
"filename": "app.log",
"extension": ".log",
"size_bytes": 425
},
{
"filename": "output.txt",
"extension": ".txt",
"size_bytes": 70,
"lines": 3
},
{
"filename": "students.json",
"extension": ".json",
"size_bytes": 522,
"lines": 11
},
{
"filename": "sample.txt",
"extension": ".txt",
"size_bytes": 127,
"lines": 5
}
],
"total_files": 12
}
# GOOD - file closes automatically
with open('data/sample.txt', 'r') as file:
content = file.read()
# AVOID - you must remember to close
# file = open('data/sample.txt', 'r')
# content = file.read()
# file.close()# Handle file not found errors
try:
with open('data/nonexistent.txt', 'r') as file:
content = file.read()
except FileNotFoundError:
print("Error: File not found!")
except Exception as e:
print(f"An error occurred: {e}")Error: File not found!
import os
filename = 'data/sample.txt'
if os.path.exists(filename):
print(f"{filename} exists!")
print(f"Size: {os.path.getsize(filename)} bytes")
else:
print(f"{filename} does not exist")data/sample.txt exists!
Size: 127 bytes
Congratulations! You’ve learned:
open(), using different modesglob using pattern matching# Reading files
with open('file.txt', 'r') as f:
content = f.read() # entire file
lines = f.readlines() # list of lines
for line in f: # iterate line by line
print(line)
# Writing files
with open('file.txt', 'w') as f: # overwrites
f.write("content")
with open('file.txt', 'a') as f: # appends
f.write("content")
# JSON
import json
with open('file.json', 'r') as f:
data = json.load(f)
with open('file.json', 'w') as f:
json.dump(data, f, indent=2)
# Glob
import glob
files = glob.glob('*.txt') # all .txt files
files = glob.glob('data/*.json') # all .json in data/pathlib module for more powerful file operationscsv moduleHappy coding!