Skip to content

Instantly share code, notes, and snippets.

@pca2
Last active March 20, 2019 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pca2/d1357d80a113cf8e68c2186c224e0593 to your computer and use it in GitHub Desktop.
Save pca2/d1357d80a113cf8e68c2186c224e0593 to your computer and use it in GitHub Desktop.
import os, re, glob
#get file list
with open('order.txt') as file:
titles = file.readlines()
#remove \n
for index, item in enumerate(titles, start=0):
titles[index] = item.rstrip()
for num, title in enumerate(titles, start=1):
glob_result = glob.glob("*{}*".format(title.rstrip()))
if bool(glob_result) & bool(num > 20):
matched_result = glob_result[0]
if re.match(r'(^\d)', matched_result):
continue
print("{} begins with \d skipping".format(matched_result))
new_filename = re.sub(r'(^)',"{} - ".format(num),matched_result)
os.rename(matched_result, new_filename)
print(new_filename)
else:
print(("{} - {} : glob failed, skipping").format(num, title))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment