import os import pathlib import subprocess import sys def rename(month): # base_directory = pathlib.Path("/mnt/nas/Photo/RAW/2022/") # base_directory = pathlib.Path("/run/user/1000/kio-fuse-rQvVbo/smb/192.168.1.77/NAS/Photo/RAW/2022/") base_directory = pathlib.Path("/mnt/c/Users/clarkzjw/Desktop/") directory = str(base_directory.joinpath(month).absolute()) + "/" files = os.listdir(directory) for file in files: if file.startswith("IMG_") and (file.endswith(".CR2") or file.endswith(".CR3")): filename = directory + file old_filename = filename.split("/")[-1].split(".")[0] img = filename if file.endswith(".CR3"): result = subprocess.run(["bash", "-c", "exiftool {} | grep '^Time Stamp'".format(img)], stdout=subprocess.PIPE) timestamp = result.stdout.decode('utf-8').strip("\n").replace(".", "-").strip("Time Stamp")[2:] \ .replace(":", "-").strip("\n") filename = directory + timestamp + "-{}.CR3".format(old_filename) elif file.endswith(".CR2"): result = subprocess.run(["bash", "-c", "exiftool {} | grep '^Date/Time Original'".format(img)], stdout=subprocess.PIPE) timestamp = result.stdout.decode('utf-8').split('\n')[1].replace(".", "-").strip("Date/Time Original")[ 2:].replace(":", "-"). \ strip("\n") filename = directory + timestamp + "-{}.CR2".format(old_filename) print(filename) os.rename(img, filename) elif file.startswith("PXL_") and file.endswith(".dng"): filename = directory + file old_filename = filename.split("/")[-1].split(".")[0] img = filename result = subprocess.run(["bash", "-c", "exiftool {} | grep '^Date/Time Original'".format(img)], stdout=subprocess.PIPE) timestamp = \ result.stdout.decode('utf-8').strip("\n").split('\n')[1].strip("Date/Time Original")[2:].split('-')[ 0][:-4].replace(".", "-").replace(":", "-").strip("\n") filename = directory + timestamp + "-{}.dng".format(old_filename) print(filename) os.rename(img, filename) if __name__ == "__main__": if len(sys.argv) <= 1: exit(0) rename(sys.argv[1])