Introduction
In this lesson, you will create a simple command-line data import/export utility utilizing Python's powerful file handling capabilities. We'll design a tool that can read and write both text and CSV files. You'll learn how to create reusable functions for handling data, manage errors gracefully, and implement user prompts for a smoother experience. By the end of this lesson, you'll have a practical utility that handles data files efficiently.
Designing the Command-line Tool
Our utility will provide easy-to-use commands to import and export data. The interface will allow users to specify file paths and choose between text or CSV formats.
Command-Line Arguments
We'll use Python's argparse module to handle command-line arguments, allowing users to specify input and output files and formats:
import argparse
parser = argparse.ArgumentParser(description='Data Import/Export Utility')
parser.add_argument('--input', type=str, help='Input file path')
parser.add_argument('--output', type=str, help='Output file path')
parser.add_argument('--format', choices=['text', 'csv'], default='csv', help='Format of the input/output file')
args = parser.parse_args()
Reading and Writing Files
The core of our utility involves reading and writing text and CSV files using Python's file modes.
Reading and Writing Text Files
We'll start with simple read and write operations using context managers to ensure files are handled safely:
# Example function to read from a text file
def read_text_file(file_path):
with open(file_path, 'r') as file:
return file.readlines()
# Example function to write to a text file
def write_text_file(file_path, data):
with open(file_path, 'w') as file:
file.writelines(data)
Reading and Writing CSV Files
For CSV operations, we'll use Python's csv module:
import csv
# Function to read from a CSV file
def read_csv_file(file_path):
with open(file_path, newline='') as csvfile:
return list(csv.reader(csvfile))
# Function to write to a CSV file
def write_csv_file(file_path, data):
with open(file_path, 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerows(data)
Reusable Functions
Our utility incorporates reusable functions for loading, transforming, and saving data, which enhances modularity and readability.
def load_data(file_path, file_format):
if file_format == 'text':
return read_text_file(file_path)
elif file_format == 'csv':
return read_csv_file(file_path)
def save_data(file_path, data, file_format):
if file_format == 'text':
write_text_file(file_path, data)
elif file_format == 'csv':
write_csv_file(file_path, data)
Error Handling
It's crucial to handle errors such as missing files, invalid paths, and malformed data gracefully, thus ensuring a robust tool:
def validate_file_path(file_path):
import os
if not os.path.isfile(file_path):
raise FileNotFoundError(f"The file {file_path} does not exist.")
try:
validate_file_path(args.input)
data = load_data(args.input, args.format)
# Apply any necessary data transformations here
save_data(args.output, data, args.format)
except (FileNotFoundError, IOError, csv.Error) as e:
print(f"Error: {e}")
Summary / Key Takeaways
In this lesson, you've learned to:
- Design a command-line utility for data import/export using Python.
- Handle file operations robustly with context managers and proper modes.
- Implement reusable functions for data loading and saving, promoting modular code.
- Gracefully handle errors such as file not found and malformed data.
- Use command-line arguments to add flexibility in selecting files and formats.
