#!/usr/bin/python3 # written by andrewt@unsw.edu.au for COMP(2041|9044) # # Change the names of the specified files # by substituting occurrances of regex with replacement # (simple version of the perl utility rename) import os import re import sys if len(sys.argv) < 3: print(f"Usage: {sys.argv[0]} [files]", file=sys.stderr) sys.exit(1) regex = sys.argv[1] replacement = sys.argv[2] for old_pathname in sys.argv[3:]: new_pathname = re.sub(regex, replacement, old_pathname, count=1) if new_pathname == old_pathname: continue if os.path.exists(new_pathname): print(f"{sys.argv[0]}: '{new_pathname}' exists", file=sys.stderr) continue try: os.rename(old_pathname, new_pathname) except OSError as e: print(f"{sys.argv[0]}: '{new_pathname}' {e}", file=sys.stderr)