Created
September 10, 2025 10:17
-
-
Save TomZhuPlanetart/4012e03d73fc63c47f381a378a94f37b to your computer and use it in GitHub Desktop.
rsync.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # mimic rsync | |
| # mainly for running on os that doesn't have rsync | |
| # python rsync.py <source_dir> <destination_dir> | |
| # | |
| import os | |
| import shutil | |
| import sys | |
| def rsync(source_dir, destination_dir): | |
| # list all files in source_dir | |
| files = os.listdir(source_dir) | |
| if not os.path.exists(destination_dir): | |
| print(f"Creating directory {destination_dir}") | |
| os.makedirs(destination_dir) | |
| for file in files: | |
| # if file is a directory, recursively rsync it | |
| if os.path.isdir(os.path.join(source_dir, file)): | |
| rsync(os.path.join(source_dir, file), os.path.join(destination_dir, file)) | |
| else: | |
| # copy only if the source file is newer than the destination file | |
| # or the destination file doesn't exist | |
| if not os.path.exists(os.path.join(destination_dir, file)) or os.path.getmtime(os.path.join(source_dir, file)) > os.path.getmtime(os.path.join(destination_dir, file)): | |
| try: | |
| print(f"Copying {os.path.join(source_dir, file)} to {os.path.join(destination_dir, file)}") | |
| shutil.copy(os.path.join(source_dir, file), os.path.join(destination_dir, file)) | |
| except Exception as e: | |
| print(f"Error copying {os.path.join(source_dir, file)} to {os.path.join(destination_dir, file)}: {e}") | |
| def main(): | |
| source_dir = sys.argv[1] | |
| destination_dir = sys.argv[2] | |
| rsync(source_dir, destination_dir) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment