Created
May 18, 2019 12:30
-
-
Save BenTristem/f3635866f79fdeb7148821d0855d39d2 to your computer and use it in GitHub Desktop.
Renames files from annoying date format to sane
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
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