Copying files with progress indicators is a practical task in Python, especially when dealing with large files or bulk operations where user feedback enhances experience. This article explores several approaches using Python libraries such as shutil, os, tqdm, and concurrent.futures to implement file copying with visible progress tracking.
Basic File Copy in Python
Python’s built-in shutil module provides basic file copying capabilities.
This method is efficient but doesn’t provide progress indicators. For enhanced functionality, a more manual approach with byte-by-byte copying is required.
Manual Copy with Progress
To create a custom copy function with progress, read and write in chunks while tracking the total size.
This function reads the file in 1MB chunks and displays real-time percentage completion in the console.
Using tqdm for Better Progress Bar
The tqdm library provides a neat progress bar in the terminal with minimal setup.
tqdm automatically manages progress display and formatting, making it user-friendly for developers.
Copy Multiple Files with Progress
When dealing with multiple files, tracking individual file progress and overall progress improves user clarity.
This function computes the total size of all files and updates the overall progress bar as each file is copied.
Asynchronous Copy with concurrent.futures
For better performance in copying large sets of files, parallelism can be introduced using ThreadPoolExecutor.
Although this version omits progress indicators for simplicity, it provides a foundation to integrate tqdm progress tracking with thread-safe updates.
Integrating Progress with Logging
In production scripts, logging progress instead of printing it to the console may be preferable.
This approach is useful in headless environments or background services where console access isn’t available.
Using rich for Stylish Terminal Output
The rich library offers aesthetically pleasing terminal outputs including progress bars.
rich enhances user experience significantly and supports nested tasks or live updates in complex CLI tools.
Error Handling and Safety Tips
While copying files, especially large or sensitive ones, consider the following best practices:
-
File existence check: Avoid overwriting unless explicitly allowed.
-
Atomic writes: Write to a temporary file first, then rename.
-
Permissions: Ensure read/write permissions for source and destination.
-
Exception handling: Gracefully handle IOErrors, disk full, etc.
Example:
When to Use What
| Scenario | Best Approach |
|---|---|
| Single large file | Manual copy with tqdm |
| Many small files | ThreadPoolExecutor with shared progress |
| CLI tools | rich progress |
| Silent background copy | Logging with logging module |
| Simple scripts | shutil.copy2() |
Conclusion
Python’s standard and third-party libraries offer flexible solutions for copying files with user-friendly progress indicators. Depending on your requirements—from simple scripts to advanced multi-threaded applications—you can choose from shutil, tqdm, rich, or even parallelization libraries to create efficient and informative file operations. Combining these tools with good practices ensures your file management tasks are both robust and user-aware.