File Handling: Advanced Concepts
In this module, we explore advanced file handling techniques in Python, including working with file paths, performing more complex input/output (I/O) operations, using context managers for cleaner code, and working with specific file formats like CSV and JSON. These concepts enable you to handle more complex file-related tasks in Python effectively.
Subtopic 1: Working with File Paths
File paths specify the location of a file or directory in the filesystem. Python's os and pathlib modules offer tools to work with file paths and perform operations like joining paths, checking file existence, and getting file attributes.
Common File Path Operations:
- Joining paths: Using
os.path.join()orpathlib.Path()to join file and directory names. - Getting the current directory:
os.getcwd()orpathlib.Path().cwd(). - Checking if a file exists:
os.path.exists()orpathlib.Path().exists().
Example (using os module):
import os
# Join path components
file_path = os.path.join('folder', 'subfolder', 'file.txt')
print(file_path) # Output: folder/subfolder/file.txt
# Check if file exists
if os.path.exists(file_path):
print(f"File exists: {file_path}")
else:
print(f"File does not exist: {file_path}")
Example (using pathlib module):
from pathlib import Path
# Create a Path object
file_path = Path('folder') / 'subfolder' / 'file.txt'
print(file_path) # Output: folder/subfolder/file.txt
# Check if file exists
if file_path.exists():
print(f"File exists: {file_path}")
else:
print(f"File does not exist: {file_path}")
Subtopic 2: File I/O Operations
File I/O (Input/Output) refers to reading from and writing to files. Advanced I/O operations involve techniques like reading large files line by line, binary reading/writing, and handling buffered and unbuffered I/O.
Buffered and Unbuffered I/O:
- Buffered I/O: This is the default behavior in Python, where data is buffered in memory before being written to or read from a file.
- Unbuffered I/O: This can be achieved by opening a file with mode
'wb'or'rb', or usingsys.stdoutfor unbuffered output.
Example: Reading Large Files Line by Line:
Reading large files line by line ensures that the file's entire content doesn't have to be loaded into memory at once.
with open('large_file.txt', 'r') as file:
for line in file:
print(line.strip())
Example: Writing in Binary Mode:
with open('binary_file.bin', 'wb') as file:
file.write(b'\x01\x02\x03\x04') # Write binary data
Subtopic 3: Using Context Managers (with Statement)
A context manager is used to manage resources (like files) efficiently. It ensures that resources are properly acquired and released, even if errors occur during file operations.
Using with Statement:
- It automatically handles opening and closing the file, eliminating the need to call
file.close()explicitly.
Example: Reading with with Statement:
with open('example.txt', 'r') as file:
content = file.read()
print(content)
# No need to explicitly call file.close() after the block
Example: Writing with with Statement:
with open('output.txt', 'w') as file:
file.write("Hello, World!")
# File is automatically closed after the block
Subtopic 4: Handling CSV and JSON Files
Python makes it easy to work with CSV and JSON files using the csv and json modules. These formats are commonly used for data storage and exchange.
CSV (Comma-Separated Values):
- Reading CSV Files: Use the
csv.reader()orcsv.DictReader()to read rows from a CSV file. - Writing CSV Files: Use the
csv.writer()orcsv.DictWriter()to write data to a CSV file.
Example: Reading CSV File:
import csv
with open('data.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row)
Example: Writing to CSV File:
import csv
data = [["Name", "Age"], ["Alice", 30], ["Bob", 25]]
with open('output.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerows(data)
JSON (JavaScript Object Notation):
- Reading JSON Files: Use
json.load()to parse a JSON file. - Writing JSON Files: Use
json.dump()to serialize data into a JSON file.
Example: Reading JSON File:
import json
with open('data.json', 'r') as file:
data = json.load(file)
print(data)
Example: Writing to JSON File:
import json
data = {"name": "Alice", "age": 30}
with open('output.json', 'w') as file:
json.dump(data, file, indent=4)
Tasks
-
Task 1: Checking File Existence
- Write a program that checks whether a file exists at a specified path. If the file exists, print its size and modification date.
-
Task 2: Reading a Large File
- Write a program to read a large text file line by line and print the first 10 lines.
-
Task 3: Writing Binary Data
- Create a binary file with some data (e.g., numbers or characters in binary format). Then, write a Python program to read and print the contents of that binary file.
-
Task 4: Working with CSV Files
- Write a Python program that reads a CSV file containing names and ages, calculates the average age, and prints the result.
-
Task 5: Handling JSON Data
- Write a program that reads a JSON file, updates a value in the data, and writes the updated data back to the file.
-
Task 6: File Copying
- Create a Python script that copies the content of one file to another. The program should handle cases where the file already exists and prompt the user to overwrite or cancel.