Skip to content

Instantly share code, notes, and snippets.

@BenTristem
Created May 18, 2019 12:30
Show Gist options
  • Save BenTristem/f3635866f79fdeb7148821d0855d39d2 to your computer and use it in GitHub Desktop.
Save BenTristem/f3635866f79fdeb7148821d0855d39d2 to your computer and use it in GitHub Desktop.
Renames files from annoying date format to sane
import os
from time import strptime
dir_path = os.path.dirname(os.path.realpath(__file__))
file_list = os.listdir(dir_path)
def rename(file):
month_name = file.split('-')[0]
month_num = str(strptime(month_name, '%b').tm_mon).zfill(2)
year_onward = file.split('-')[1]
new_name = '{}-{}{}'.format(
year_onward[:4],
month_num,
year_onward[4:]
)
os.rename(file, new_name)
for file in file_list:
if file.endswith('.pdf'):
rename(file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment