dirsync

2.2.6 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `sync` function to synchronize files from a source directory to a target directory. It includes creating dummy directories and files, performing a 'sync' operation with verbose output and target directory creation, and finally cleaning up.

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)

view raw JSON →