dirsync
dirsync is an advanced Python library and command-line tool for synchronizing directory trees. It is based on Anand B Pillai's Python robocopier and provides functionalities like reporting differences, syncing content, and updating existing content with various options for filtering and behavior. The current version is 2.2.6, and it appears to have a stable, though infrequent, release cadence.
Common errors
-
TypeError: sync() missing 1 required positional argument: 'action'
cause The `action` argument (e.g., 'sync', 'diff', 'update') was not provided when calling the `dirsync.sync` function.fixSpecify the synchronization action as the third positional argument: `from dirsync import sync; sync('source_dir', 'target_dir', 'sync')`. -
IOError: [Errno 2] No such file or directory: 'target_data'
cause The target directory specified for synchronization does not exist, and the `create` option was not enabled.fixEither create the target directory manually before calling `sync`, or pass the `create=True` argument: `sync('source_dir', 'target_dir', 'sync', create=True)`. -
Files are not being filtered correctly with `include` or `exclude` options.
cause The regex patterns provided to `include` or `exclude` do not match the full file paths, or they were not passed as a list of strings when using the Python API.fixEnsure regex patterns are anchored (`^` and `$`) to match the full path (e.g., `r'^.*\.py$'`). When using the Python API, provide patterns as a list: `sync(..., include=['^.*\.py$'])`.
Warnings
- gotcha The `action` parameter (e.g., 'sync', 'diff', 'update') is mandatory when calling `dirsync.sync` from Python or using the command-line tool. Omitting it will result in an error or unexpected behavior.
- gotcha By default, the target directory must exist. If it doesn't, `dirsync` will fail. To automatically create the target directory if it's missing, you must explicitly use the `create=True` option in the Python API or `--create` flag in the CLI.
- gotcha When using `ignore`, `only`, `exclude`, or `include` options, the patterns provided are regular expressions and must match the *entire path* from the beginning. For the Python API, these options expect a list of regex strings.
- gotcha Configuration files (`.dirsync` in user's home or source/target directories) are only parsed and used when `dirsync` is run from the command line. They are ignored when the `sync` function is called directly from Python code.
Install
-
pip install dirsync
Imports
- sync
from dirsync import sync
Quickstart
import os
from dirsync import sync
# Create dummy source and target directories for demonstration
source_dir = './source_data'
target_dir = './target_data'
os.makedirs(source_dir, exist_ok=True)
os.makedirs(target_dir, exist_ok=True)
with open(os.path.join(source_dir, 'file1.txt'), 'w') as f:
f.write('Hello from source!')
with open(os.path.join(source_dir, 'file2.log'), 'w') as f:
f.write('Log content.')
print(f"Synchronizing from '{source_dir}' to '{target_dir}'")
# Sync: copies all files from source to target, creating target if it doesn't exist
sync(source_dir, target_dir, 'sync', verbose=True, create=True)
print(f"Content of '{target_dir}': {os.listdir(target_dir)}")
# Clean up (optional)
os.remove(os.path.join(source_dir, 'file1.txt'))
os.remove(os.path.join(source_dir, 'file2.log'))
os.rmdir(source_dir)
os.remove(os.path.join(target_dir, 'file1.txt'))
os.remove(os.path.join(target_dir, 'file2.log'))
os.rmdir(target_dir)