Copying and Moving Files in Python: Working with shutil and os Modules
Python development often requires file and directory operations. Creating backups, handling user uploads, organizing project structures, and deploying applications all demand programmatic file system management. Automating these tasks significantly simplifies data management and eliminates the need for repetitive manual work.
The shutil module in Python provides high-level utilities for working with files and directories. Combined with the os module, it forms a powerful set of cross-platform tools for building flexible file system management scripts. Both modules are part of Python's standard library and are available without additional installation.
Overview of the shutil Module
The shutil module, named after "shell utility," offers a collection of functions for high-level file and directory operations. Key functions include:
copy()— copies a file without preserving metadatacopy2()— copies a file while preserving all metadatacopytree()— recursively copies entire directoriesmove()— moves files and folders to a new locationrmtree()— recursively removes directories and their contentscopyfileobj()— copies file objects byte by byte
The shutil module integrates seamlessly with the os module and is suitable for both small automation scripts and large enterprise-level applications.
File Copying Methods
Basic Copying with shutil.copy()
The shutil.copy() function performs a simple copy of file contents without transferring metadata:
import shutil
source = 'source_folder/example.txt'
destination = 'destination_folder/example.txt'
shutil.copy(source, destination)
This function copies only the file's content. File metadata, including creation date, last modification time, and permissions, is not preserved. The function is similar to the cp command in Unix/Linux systems without attribute preservation flags.
Keep in mind that if the destination directory does not exist, an error will occur. It's recommended to check the existence of the target directory beforehand or create it programmatically.
Copying with Metadata Preservation: shutil.copy2()
When you need to preserve all file information, including timestamps, owner, and permissions, use the copy2() function:
shutil.copy2('file.txt', 'backup/file.txt')
This method is especially useful for creating backups, migrating projects between different systems, or when preserving original file attributes is critical. The copy2() function ensures the most accurate reproduction of the source file in the new location.
Copying Directories
Recursive Copying with shutil.copytree()
To copy entire directories along with all nested subdirectories and files, use the copytree() function:
shutil.copytree('old_project', 'new_project')
By default, copytree() raises a FileExistsError if the target directory already exists. Starting from Python 3.8, you can use the dirs_exist_ok=True parameter to allow overwriting or merging content:
shutil.copytree('old_project', 'new_project', dirs_exist_ok=True)
The function works recursively and is used in the following scenarios:
- Cloning project templates