1. Introduction to the basic tool library
In Python standard libraryshutil
Modules are the main tools:
import shutil import os
2. File copy basics
1. Copy a single file
# Retain metadata (modification time, etc.)shutil.copy2('', '') # Copy content only('', 'backup/') # Automatically retain file names # File flow method (suitable for large files)with open('', 'rb') as src, open('', 'wb') as dst: (src, dst, length=16*1024) #16KB buffer
3. Advanced directory copying
1. Simple directory copying
# Copy the entire directory (the target directory must not exist)('src_dir', 'dst_dir') # Allow overwrite existing directories (Python 3.8+)('src', 'existing_dir', dirs_exist_ok=True)
2. Customize the copy process
def ignore_patterns(*patterns): def _ignore(path, names): ignored = [] for pattern in patterns: ((names, pattern)) return set(ignored) return _ignore # Exclude .pyc files and temporary files('src', 'dst', ignore=ignore_patterns('*.pyc', '*.tmp')) # Copy with progress callbacksdef copy_progress(src, dst, *, follow_symlinks=True): print(f"Copying {src} => {dst}") ('src', 'dst', copy_function=copy_progress)
4. Advanced custom implementation
When full control of the replication process is required, it can be done manually:
def deep_copy(src, dst, symlinks=False): if not (dst): (dst) (src, dst) for item in (src): src_path = (src, item) dst_path = (dst, item) if (src_path): deep_copy(src_path, dst_path, symlinks) else: if (dst_path): if (src_path, dst_path): continue (dst_path) if symlinks and (src_path): linkto = (src_path) (linkto, dst_path) else: shutil.copy2(src_path, dst_path)
5. Key points for exception handling
try: ('src', 'dst') except as e: print(f'Directory not copied. Error: {e}') except OSError as e: print(f'OSError: {}')
6. Performance optimization suggestions
- Batch small files: Use
The default implementation of
- Extra large file: Use
copyfileobj
Block copy - Network storage: Increase the buffer size (e.g.
16*1024*1024
That is 16MB) - Parallel processing: Use multi-threaded/multi-process for independent subdirectories
7. Complete sample code
import shutil import os from pathlib import Path def smart_copy(src, dst, overwrite=False, ignore=None): """Smart Copyer""" src = Path(src) dst = Path(dst) if src.is_file(): if dst.is_dir(): dst = dst / if (): if overwrite: () else: raise FileExistsError(f"{dst} already exists") shutil.copy2(str(src), str(dst)) return if not src.is_dir(): raise ValueError("Source path invalid") if (): if overwrite: if dst.is_dir(): (dst) else: () else: raise FileExistsError(f"{dst} already exists") ( str(src), str(dst), symlinks=True, ignore=ignore, copy_function=shutil.copy2, dirs_exist_ok=overwrite ) #User Examplesmart_copy( '/data/project', '/backup/project_2024', overwrite=True, ignore=shutil.ignore_patterns('*.log', 'temp') )
Key points description:
- use
copy2
Insteadcopy
File metadata can be preserved -
Path
Objects are safer and easier to use than string paths -
dirs_exist_ok
Parameters require Python 3.8+ - Custom ignore mode supports complex filtering logic
- Perfect exception handling ensures operational reliability
This approach can handle the following complex scenarios:
- Mixed file types (normal file/symbol link/special file)
- Keep all file attributes
- Security processing of existing content
- Flexible filtering mechanism
- Cross-platform compatibility (Windows/Unix)
The above is the detailed content of Python's implementation of file/folder copying function. For more information about Python file/folder copying, please follow my other related articles!