Created
October 11, 2016 19:22
-
-
Save dennissergeev/f1594a2039dbc5ce31b315dac8de1917 to your computer and use it in GitHub Desktop.
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 | |
import argparse | |
import os | |
ap = argparse.ArgumentParser(os.path.basename(__file__), | |
description=__doc__, | |
formatter_class=argparse. | |
ArgumentDefaultsHelpFormatter) | |
ap.add_argument('path_to_files', type=str, help='Path to files') | |
ap.add_argument('-z', '--zeros', type=int, default=4, | |
help='Number of zeros') | |
if __name__ == '__main__': | |
args = ap.parse_args() | |
path = args.path_to_files | |
files = os.listdir(path) | |
for f in files: | |
name, ext = os.path.splitext(f) | |
pref, num = name.split() | |
new_num = num.zfill(args.zeros) | |
new_name = '{}_{}{}'.format(pref, new_num, ext) | |
os.rename(os.path.join(path, f), | |
os.path.join(path, new_name)) | |
print('Renamed {} to {}'.format(os.path.join(path, f), | |
os.path.join(path, new_name))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment