Created
March 18, 2020 18:24
-
-
Save twitu/8923d54278e3e4a75522cc27088f6256 to your computer and use it in GitHub Desktop.
Pad numeral part in file name to correctly order files
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/python3 | |
import os | |
import re | |
import sys | |
import argparse | |
def match_files(regex): | |
return [name for name in os.listdir() if regex.match(name)] | |
# regex should group on the numeral part of file name | |
def pad_length(regex, files): | |
max_num = max([int(regex.search(filename).group(1)) for filename in files]) | |
return len(str(max_num)) | |
def new_file_names(pad, files): | |
matches = [(regex.search(filename).group(1), filename) for filename in files] | |
return [name.replace(m, m.zfill(pad)) for (m, name) in matches] | |
def rename_files(names, new_names): | |
for (name, new_name) in zip(names, new_names): | |
os.rename(name, new_name) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='Pad numbers in file name') | |
parser.add_argument("regex", help='regex pattern for file which groups numerical pattern. for e.g. playlist_(\d*)_.*_album.mp3', type=str, action='store') | |
parser.add_argument('--debug', '-d', help='perform a dry run without actually changing file names', action='store_true') | |
args = parser.parse_args() | |
regex = re.compile(args.regex) | |
debug = args.debug | |
names = match_files(regex) | |
pad = pad_length(regex, names) | |
new_names = new_file_names(pad, names) | |
if debug: | |
print("{} files found".format(len(names))) | |
print('\n'.join(names)) | |
print() | |
print("New names") | |
print('\n'.join(new_names)) | |
else: | |
rename_files(names, new_names) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
chmod +x pad_file_name.py
mv pad_file_name.py /usr/bin/
cd directory/where/files/names/are/not/padded
Example
pad_file_name.py --debug "playlist_(\d+)_.*_album.mp3"
pad_file_name.py "playlist_(\d+)_.*_album.mp3"