Last active
March 14, 2025 11:59
-
-
Save zoomlogo/396fc31bac44665a1bfdd6eb54c8d5c2 to your computer and use it in GitHub Desktop.
simple script to compare directories
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
#!/usr/bin/python | |
# compare two directories | |
import os | |
copy = __import__("shutil").copy2 | |
import sys; sys.argv = sys.argv[1:] | |
from colorama import Fore, Style | |
if len(sys.argv) == 1 and ("help" in sys.argv or "h" in sys.argv): | |
print("usage: compare <old_dir> <new_dir>") | |
print(" 2 : compare (c)opy <copy_dir> <old_dir> <new_dir>") | |
print(" ^~~ copies the new files in new_dir to copy_dir") | |
sys.exit(0) | |
copy_dir = None | |
if len(sys.argv) != 0 and sys.argv[0] in ["c", "copy"]: | |
copy_dir = sys.argv[1] | |
sys.argv = sys.argv[2:] | |
if len(sys.argv) != 2: | |
print("error: too few/much arguments provided. atleast 2 expected.", file=sys.stderr) | |
sys.exit(1) | |
files = [] | |
for directory in sys.argv: | |
try: | |
files.append(os.listdir(directory)) | |
except FileNotFoundError: | |
print(f"error: {directory} not found.", file=sys.stderr) | |
sys.exit(1) | |
files1 = files[0] | |
files2 = files[1] | |
# new files | |
print(end=Fore.GREEN) | |
for file in files2: | |
if file not in files1: | |
print(f"+ {file}") | |
if copy_dir is not None: | |
copy(sys.argv[1] + "/" + file, copy_dir) | |
print(end=Style.RESET_ALL) | |
# removed files | |
print(end=Fore.RED) | |
for file in files1: | |
if file not in files2: | |
print(f"- {file}") | |
print(end=Style.RESET_ALL) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment