Last active
September 5, 2020 02:40
-
-
Save i5ar/758b6d5669f39cb5e7ffd47a1e57b05c to your computer and use it in GitHub Desktop.
Rename DJI images
This file contains 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/env python3 | |
# ________ ____ ____ _____ ___ ___ | |
# / ___/ _ \/ __ \/ __ `/ __ `__ \/ _ \ | |
# / / / __/ / / / /_/ / / / / / / __/ | |
# /_/ \___/_/ /_/\__,_/_/ /_/ /_/\___/ | |
""" | |
Replace a character from 'old' to 'new' at specific 'index' | |
Basic usage: | |
>>> python rename.py -i 4 -c 1 -e jpg | |
""" | |
import pathlib | |
import argparse | |
parser = argparse.ArgumentParser(description='Process some integers.') | |
parser.add_argument('--index', '-i', required=True, type=int, help='index') | |
parser.add_argument('--character', '-c', required=True, help='character') | |
parser.add_argument('--extension', '-e', required=True, help='extension') | |
args = parser.parse_args() | |
index = args.index | |
character = args.character | |
extension = args.extension | |
pattern = f'*.{extension}' if extension else '*' | |
files = pathlib.Path().glob(pattern) | |
for file in files: | |
if file.is_file(): | |
old_stem = file.stem | |
try: | |
old_extension = file.suffix | |
new_stem = old_stem[:index] + character + old_stem[index+1:] | |
file.rename(pathlib.Path('.', new_stem + old_extension)) | |
except IndexError as err: | |
print(err) | |
except FileExistsError as err: | |
print(err) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment