Directory Script [exclusive] · Full & Recent
batch_rename("./photos", r"\d4-\d2-\d2_", "") import time from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler class MyHandler(FileSystemEventHandler): def on_modified(self, event): print(f"Modified: event.src_path") def on_created(self, event): print(f"Created: event.src_path")
if == " main ": list_files(sys.argv[1] if len(sys.argv) > 1 else ".") Batch rename files import os import re def batch_rename(directory, pattern, replacement): for filename in os.listdir(directory): new_name = re.sub(pattern, replacement, filename) if new_name != filename: os.rename( os.path.join(directory, filename), os.path.join(directory, new_name) ) print(f"Renamed: filename → new_name") directory script
# Your logic here for item in target.iterdir(): if args.verbose: print(f"Processing: item") # ... do something if == " main ": main() 9. Quick Reference: Path Operations | Language | Get current dir | Join path | Check exists | List files | |----------|----------------|-----------|--------------|------------| | Bash | pwd | $dir/$file | [ -d "$path" ] | ls | | Python | os.getcwd() | os.path.join() | os.path.exists() | os.listdir() | | PowerShell | Get-Location | Join-Path | Test-Path | Get-ChildItem | Need a script for a specific directory task ? Tell me the task and your OS, and I'll write it for you. batch_rename("
def main(): parser = argparse.ArgumentParser(description="Process a directory") parser.add_argument("directory", help="Target directory") parser.add_argument("--recursive", "-r", action="store_true") parser.add_argument("--verbose", "-v", action="store_true") args = parser.parse_args() Tell me the task and your OS, and I'll write it for you
target = Path(args.directory) if not target.exists(): print(f"Error: target does not exist", file=sys.stderr) sys.exit(1) if not target.is_dir(): print(f"Error: target is not a directory", file=sys.stderr) sys.exit(1)