Python File Handling
1. Introduction to File Handling
File handling in Python allows programs to create, read, write, and manage files on disk. Python
provides built-in functions and methods for working with files, making it one of the most practical
features for real-world applications.
open() Built-in function to open a file
read() Read file contents
write() Write data to a file
close() Close the file handle
with statement Context manager — auto-closes file
2. File Open Modes
Python's open() function accepts a mode parameter that controls how the file is opened.
Mode Name Description
'r' Read Default. Opens file for reading. Error if file doesn't exist.
'w' Write Opens for writing. Creates file if not exists. Truncates
existing.
'a' Append Opens for appending. Creates file if not exists.
'x' Exclusive Creates a new file. Error if file already exists.
'r+' Read+Write Opens for reading and writing. File must exist.
'w+' Write+Read Opens for writing and reading. Truncates existing.
'a+' Append+Read Opens for appending and reading.
'b' Binary Binary mode. Used with other modes e.g. 'rb', 'wb'.
't' Text Text mode (default). Used with other modes e.g. 'rt'.
3. Writing to Files
3.1 write() – Write a String
The write() method writes a string to the file. It does not add a newline automatically.
Code:
# Writing to a file using 'w' mode
file = open('[Link]', 'w')
[Link]('Hello, World!\n')
[Link]('Python File Handling\n')
[Link]('Line 3\n')
[Link]()
print('File written successfully')
Output:
File written successfully
3.2 writelines() – Write a List of Strings
writelines() writes a list (or any iterable) of strings to the file.
Code:
lines = ['First line\n', 'Second line\n', 'Third line\n']
with open('[Link]', 'w') as f:
[Link](lines)
print('Lines written successfully')
Output:
Lines written successfully
3.3 Append Mode ('a')
Append mode adds content to the end of an existing file without overwriting it.
Code:
# First write some content
with open('[Link]', 'w') as f:
[Link]('Log Entry 1\n')
# Now append to the file
with open('[Link]', 'a') as f:
[Link]('Log Entry 2\n')
[Link]('Log Entry 3\n')
# Read and verify
with open('[Link]', 'r') as f:
print([Link]())
Output:
Log Entry 1
Log Entry 2
Log Entry 3
4. Reading from Files
4.1 read() – Read Entire File
read() reads the entire file content as a single string.
Code:
with open('[Link]', 'r') as f:
content = [Link]()
print(content)
print('Total characters:', len(content))
Output:
Hello, World!
Python File Handling
Line 3
Total characters: 40
4.2 read(n) – Read n Characters
You can pass a number to read() to read only that many characters.
Code:
with open('[Link]', 'r') as f:
chunk1 = [Link](5) # Read first 5 chars
chunk2 = [Link](8) # Read next 8 chars
print('Chunk 1:', repr(chunk1))
print('Chunk 2:', repr(chunk2))
Output:
Chunk 1: 'Hello'
Chunk 2: ', World'
4.3 readline() – Read One Line
readline() reads a single line from the file, including the trailing newline character.
Code:
with open('[Link]', 'r') as f:
line1 = [Link]()
line2 = [Link]()
print('Line 1:', repr(line1))
print('Line 2:', repr(line2))
Output:
Line 1: 'Hello, World!\n'
Line 2: 'Python File Handling\n'
4.4 readlines() – Read All Lines into a List
readlines() returns a list where each element is one line from the file.
Code:
with open('[Link]', 'r') as f:
lines = [Link]()
print('All lines:', lines)
print('Number of lines:', len(lines))
Output:
All lines: ['Hello, World!\n', 'Python File Handling\n', 'Line 3\n']
Number of lines: 3
4.5 Iterating Line by Line (Most Efficient)
Iterating directly over a file object is memory-efficient and Pythonic.
Code:
with open('[Link]', 'r') as f:
for i, line in enumerate(f, start=1):
print(f'Line {i}: {[Link]()}')
Output:
Line 1: Hello, World!
Line 2: Python File Handling
Line 3: Line 3
5. The 'with' Statement (Context Manager)
The with statement ensures that the file is properly closed after use, even if an exception occurs. It is
the recommended way to handle files in Python.
Code:
# Without 'with' - manual close required
f = open('[Link]', 'w')
[Link]('Some data')
[Link]() # Must remember to close!
# With 'with' - auto closes
with open('[Link]', 'w') as f:
[Link]('Some data')
# File is automatically closed here
print('Is file closed?', [Link])
Output:
Is file closed? True
You can also open multiple files in a single with statement:
Code:
with open('[Link]', 'r') as fin, open('[Link]', 'w') as fout:
data = [Link]()
[Link]([Link]())
print('File copied and converted to uppercase')
Output:
File copied and converted to uppercase
6. File Pointer – tell() and seek()
The file pointer tracks the current position in the file. tell() returns the current position; seek() moves
it.
6.1 tell() – Current Position
Code:
with open('[Link]', 'r') as f:
print('Start position:', [Link]())
[Link](5)
print('After reading 5 chars:', [Link]())
[Link](10)
print('After reading 10 more:', [Link]())
Output:
Start position: 0
After reading 5 chars: 5
After reading 10 more: 15
6.2 seek() – Move File Pointer
seek(offset, whence) moves the pointer. whence=0 (start), 1 (current), 2 (end).
Code:
with open('[Link]', 'r') as f:
[Link](7) # Move to position 7
print([Link](5)) # Read 5 chars from position 7
[Link](0) # Go back to start
print([Link](5)) # Read first 5 chars again
[Link](0, 2) # Go to end of file
print('End position:', [Link]())
Output:
World
Hello
End position: 40
7. File Operations with os Module
The os module provides functions to interact with the operating system — rename, delete, check
existence, and more.
7.1 Checking File Existence
Code:
import os
# Check if file exists
print([Link]('[Link]')) # True if file exists
print([Link]('[Link]')) # True if it's a file
print([Link]('[Link]')) # True if it's a directory
Output:
True
True
False
7.2 Renaming and Deleting Files
Code:
import os
# Create a file
with open('old_name.txt', 'w') as f:
[Link]('Temporary file')
# Rename it
[Link]('old_name.txt', 'new_name.txt')
print('Renamed:', [Link]('new_name.txt'))
# Delete it
[Link]('new_name.txt')
print('Deleted:', not [Link]('new_name.txt'))
Output:
Renamed: True
Deleted: True
7.3 File Info – size, path, name
Code:
import os
path = '[Link]'
print('File size:', [Link](path), 'bytes')
print('Absolute path:', [Link](path))
print('File name:', [Link](path))
print('Directory:', [Link]([Link](path)))
print('Name & Ext:', [Link](path))
Output:
File size: 40 bytes
Absolute path: /home/user/[Link]
File name: [Link]
Directory: /home/user
Name & Ext: ('example', '.txt')
8. Binary File Handling
Binary mode ('b') is used for non-text files like images, audio, and executables.
8.1 Writing and Reading Binary Files
Code:
# Writing binary data
data = bytes([72, 101, 108, 108, 111]) # 'Hello' in ASCII
with open('[Link]', 'wb') as f:
[Link](data)
print('Binary data written')
# Reading binary data
with open('[Link]', 'rb') as f:
content = [Link]()
print('Bytes:', content)
print('Decoded:', [Link]('utf-8'))
Output:
Binary data written
Bytes: b'Hello'
Decoded: Hello
8.2 Copying a Binary File
Code:
def copy_file(src, dst, chunk_size=1024):
with open(src, 'rb') as fin:
with open(dst, 'wb') as fout:
while True:
chunk = [Link](chunk_size)
if not chunk:
break
[Link](chunk)
print(f'Copied {src} -> {dst}')
copy_file('[Link]', 'binary_copy.bin')
Output:
Copied [Link] -> binary_copy.bin
9. Exception Handling with Files
File operations can fail for many reasons. Always use try-except blocks for robust code.
9.1 FileNotFoundError
Code:
try:
with open('[Link]', 'r') as f:
content = [Link]()
except FileNotFoundError as e:
print('Error: File not found!')
print('Details:', e)
Output:
Error: File not found!
Details: [Errno 2] No such file or directory: '[Link]'
9.2 PermissionError and IOError
Code:
try:
with open('[Link]', 'r') as f:
data = [Link]()
print('Read', len(data), 'characters')
except PermissionError:
print('Permission denied!')
except IOError as e:
print('I/O Error:', e)
except Exception as e:
print('Unexpected error:', e)
else:
print('File read successfully!')
finally:
print('This always executes')
Output:
Read 40 characters
File read successfully!
This always executes
10. CSV File Handling
Python's csv module makes reading and writing comma-separated value files easy.
10.1 Writing a CSV File
Code:
import csv
data = [
['Name', 'Age', 'City'],
['Alice', 30, 'New York'],
['Bob', 25, 'London'],
['Charlie', 35, 'Tokyo'],
]
with open('[Link]', 'w', newline='') as f:
writer = [Link](f)
[Link](data)
print('CSV file created')
Output:
CSV file created
10.2 Reading a CSV File
Code:
import csv
with open('[Link]', 'r') as f:
reader = [Link](f)
for row in reader:
print(row)
Output:
['Name', 'Age', 'City']
['Alice', '30', 'New York']
['Bob', '25', 'London']
['Charlie', '35', 'Tokyo']
10.3 DictReader & DictWriter
Code:
import csv
# Read as dictionaries
with open('[Link]', 'r') as f:
reader = [Link](f)
for row in reader:
print(f"{row['Name']} is {row['Age']} years old from {row['City']}")
Output:
Alice is 30 years old from New York
Bob is 25 years old from London
Charlie is 35 years old from Tokyo
11. JSON File Handling
The json module allows you to serialize Python objects to JSON files and deserialize them back.
11.1 Writing JSON
Code:
import json
data = {
'name': 'Alice',
'age': 30,
'hobbies': ['reading', 'coding'],
'address': {'city': 'New York', 'zip': '10001'}
}
with open('[Link]', 'w') as f:
[Link](data, f, indent=4)
print('JSON file written')
Output:
JSON file written
# [Link] content:
{
"name": "Alice",
"age": 30,
"hobbies": ["reading", "coding"],
"address": {"city": "New York", "zip": "10001"}
}
11.2 Reading JSON
Code:
import json
with open('[Link]', 'r') as f:
loaded = [Link](f)
print('Name:', loaded['name'])
print('Age:', loaded['age'])
print('City:', loaded['address']['city'])
print('Hobbies:', loaded['hobbies'])
print('Type:', type(loaded))
Output:
Name: Alice
Age: 30
City: New York
Hobbies: ['reading', 'coding']
Type: <class 'dict'>
12. pathlib – Modern File Path Handling
pathlib (Python 3.4+) provides an object-oriented approach to filesystem paths.
12.1 Basic Path Operations
Code:
from pathlib import Path
p = Path('[Link]')
print('Name:', [Link])
print('Stem:', [Link]) # name without extension
print('Suffix:', [Link]) # extension
print('Parent:', [Link])
print('Absolute:', [Link]())
print('Exists:', [Link]())
print('Is file:', p.is_file())
Output:
Name: [Link]
Stem: example
Suffix: .txt
Parent: .
Absolute: /home/user/[Link]
Exists: True
Is file: True
12.2 Reading & Writing with pathlib
Code:
from pathlib import Path
p = Path('pathlib_test.txt')
# Write
p.write_text('Hello from pathlib!\nLine 2')
# Read
content = p.read_text()
print(content)
# List all .txt files in current directory
txt_files = list(Path('.').glob('*.txt'))
print('TXT files found:', len(txt_files))
Output:
Hello from pathlib!
Line 2
TXT files found: 5
13. Temporary Files
The tempfile module creates temporary files and directories that are automatically deleted.
Code:
import tempfile
import os
# Create a temporary file
with [Link](mode='w', suffix='.txt', delete=False) as tmp:
[Link]('Temporary content\n')
[Link]('This will be deleted\n')
tmp_name = [Link]
print('Temp file:', tmp_name)
# Read from it
with open(tmp_name, 'r') as f:
print([Link]())
# Clean up
[Link](tmp_name)
print('Temp file deleted')
Output:
Temp file: /tmp/[Link]
Temporary content
This will be deleted
Temp file deleted
14. File Operations with shutil
The shutil module provides high-level file operations like copying, moving, and archiving.
Code:
import shutil
import os
# Copy a file
[Link]('[Link]', 'example_copy.txt')
print('File copied')
# Copy with metadata
shutil.copy2('[Link]', 'example_meta.txt')
print('File copied with metadata')
# Move a file
[Link]('example_meta.txt', 'moved_file.txt')
print('File moved')
# Get disk usage
usage = shutil.disk_usage('.')
print(f'Total: {[Link] // (2**30)} GB')
print(f'Used: {[Link] // (2**30)} GB')
print(f'Free: {[Link] // (2**30)} GB')
Output:
File copied
File copied with metadata
File moved
Total: 256 GB
Used: 128 GB
Free: 127 GB
15. Practical Example – Log File Processor
A real-world example that reads a log file, processes it, and writes a summary report.
Code:
import json
from datetime import datetime
from collections import Counter
# Simulate a log file
log_data = '''
2024-01-15 ERROR Database connection failed
2024-01-15 INFO Server started
2024-01-15 WARNING Disk space low
2024-01-15 ERROR Null pointer exception
2024-01-15 INFO Request processed
2024-01-15 ERROR Timeout after 30s
'''
with open('[Link]', 'w') as f:
[Link](log_data.strip())
# Process the log
level_counts = Counter()
errors = []
with open('[Link]', 'r') as f:
for line in f:
parts = [Link]().split(' ', 2)
if len(parts) == 3:
date, level, msg = parts
level_counts[level] += 1
if level == 'ERROR':
[Link](msg)
# Write summary report
summary = {
'generated': [Link]().isoformat(),
'counts': dict(level_counts),
'errors': errors
}
with open('[Link]', 'w') as f:
[Link](summary, f, indent=2)
print('=== Log Summary ===')
for level, count in level_counts.items():
print(f'{level:10s}: {count}')
print(f'\nErrors found: {len(errors)}')
print('Report saved to [Link]')
Output:
=== Log Summary ===
ERROR : 3
INFO : 2
WARNING : 1
Errors found: 3
Report saved to [Link]
16. Quick Reference Summary
Operation Code
Open for reading open('[Link]', 'r')
Open for writing open('[Link]', 'w')
Open for appending open('[Link]', 'a')
Read all content [Link]()
Read one line [Link]()
Read all lines [Link]()
Write string [Link]('text')
Write list [Link](list)
Get position [Link]()
Set position [Link](0)
Check exists [Link]('[Link]')
Delete file [Link]('[Link]')
Rename file [Link]('old', 'new')
File size [Link]('[Link]')
Copy file [Link]('src', 'dst')
Read JSON [Link](f)
Write JSON [Link](data, f, indent=4)
pathlib read Path('file').read_text()
pathlib write Path('file').write_text('data')
PROGRAM 1: Count Words
def count_words(filename):
try:
with open(filename, 'r', encoding='utf-8') as file:
content = [Link]()
words = [Link]()
print(f'Total words: {len(words)}')
return len(words)
except FileNotFoundError:
print(f'Error: File not found')
# Usage
count_words('[Link]')
PROGRAM 2: File Analysis
def analyze_file(filename):
try:
with open(filename, 'r', encoding='utf-8') as file:
content = [Link]()
lines = [Link]('\n')
words = [Link]()
print(f'File: {filename}')
print(f'Lines: {len(lines)}')
print(f'Words: {len(words)}')
print(f'Characters: {len(content)}')
except FileNotFoundError:
print('File not found')
# Usage
analyze_file('[Link]')
PROGRAM 3: Copy File
def copy_file(source, destination):
try:
with open(source, 'r') as src:
content = [Link]()
with open(destination, 'w') as dst:
[Link](content)
print(f'File copied: {source} -> {destination}')
except FileNotFoundError:
print('Source file not found')
# Usage
copy_file('[Link]', '[Link]')
PROGRAM 4: Find and Replace
def find_and_replace(filename, old, new):
try:
with open(filename, 'r') as f:
content = [Link]()
count = [Link](old)
updated = [Link](old, new)
with open(filename, 'w') as f:
[Link](updated)
print(f'Found and replaced {count} occurrence(s)')
except FileNotFoundError:
print('File not found')
# Usage
find_and_replace('[Link]', 'old', 'new')
PROGRAM 5: Word Frequency
from collections import Counter
import string
def word_frequency(filename, top=10):
try:
with open(filename, 'r') as f:
content = [Link]().lower()
for p in [Link]:
content = [Link](p, ' ')
words = [Link]()
freq = Counter(words)
print('Top words:')
for word, count in freq.most_common(top):
print(f'{word}: {count}')
except FileNotFoundError:
print('File not found')
# Usage
word_frequency('[Link]')
PROGRAM 6: Search Pattern
def search_pattern(filename, pattern):
try:
with open(filename, 'r') as f:
lines = [Link]()
matches = []
for i, line in enumerate(lines, 1):
if [Link]() in [Link]():
[Link]((i, [Link]()))
print(f'Found {len(matches)} match(es)')
for line_num, content in matches:
print(f'Line {line_num}: {content}')
except FileNotFoundError:
print('File not found')
# Usage
search_pattern('[Link]', 'python')
PROGRAM 7: Sort Lines
def sort_lines(filename, output=None):
try:
with open(filename, 'r') as f:
lines = [Link]()
sorted_lines = sorted(lines)
if output is None:
output = [Link]('.txt', '_sorted.txt')
with open(output, 'w') as f:
[Link](sorted_lines)
print(f'Sorted {len(lines)} lines to {output}')
except FileNotFoundError:
print('File not found')
# Usage
sort_lines('[Link]')
PROGRAM 8: Remove Duplicates
def remove_duplicates(filename, output=None):
try:
with open(filename, 'r') as f:
lines = [Link]()
unique = []
seen = set()
for line in lines:
stripped = [Link]()
if stripped and stripped not in seen:
[Link](line)
[Link](stripped)
if output is None:
output = [Link]('.txt', '_unique.txt')
with open(output, 'w') as f:
[Link](unique)
print(f'Removed {len(lines) - len(unique)} duplicates')
except FileNotFoundError:
print('File not found')
# Usage
remove_duplicates('[Link]')
PROGRAM 9: Read Specific Lines
def read_lines_range(filename, start, end):
try:
with open(filename, 'r') as f:
lines = [Link]()
if start < 1 or end < 1 or start > len(lines):
print('Invalid range')
return
end = min(end, len(lines))
print(f'Lines {start} to {end}:')
for i in range(start-1, end):
print(f'{i+1}: {lines[i].rstrip()}')
except FileNotFoundError:
print('File not found')
# Usage
read_lines_range('[Link]', 5, 15)
PROGRAM 10: Append Lines
def append_lines(filename, lines_list):
try:
with open(filename, 'a') as f:
for line in lines_list:
[Link](line + '\n')
print(f'Appended {len(lines_list)} lines')
except Exception as e:
print(f'Error: {e}')
# Usage
lines = ['New entry 1', 'New entry 2']
append_lines('[Link]', lines)
PROGRAM 11: Merge Files
def merge_files(file_list, output):
try:
with open(output, 'w') as outf:
for filename in file_list:
try:
with open(filename, 'r') as inf:
[Link]([Link]())
[Link]('\n' + '='*50 + '\n')
except FileNotFoundError:
print(f'Skipped: {filename}')
print(f'Merged {len(file_list)} files to {output}')
except Exception as e:
print(f'Error: {e}')
# Usage
merge_files(['[Link]', '[Link]'], '[Link]')
PROGRAM 12: Reverse File
def reverse_lines(filename, output=None):
try:
with open(filename, 'r') as f:
lines = [Link]()
reversed_lines = lines[::-1]
if output is None:
output = [Link]('.txt', '_reversed.txt')
with open(output, 'w') as f:
[Link](reversed_lines)
print(f'Reversed {len(lines)} lines to {output}')
except FileNotFoundError:
print('File not found')
# Usage
reverse_lines('[Link]')