I recently started with python since I want to become better in geophysics and processing data.
I began with a little sideproject to rename the monthly extract files of my bank account. They have a silly order of date and name and I just want to order them by date.
One of the file names would be AZG149882014_001_20170129.
The last 8 symbols are the date, but in a very unintuitive order, where my winodws system cannot order them properly.
So far the file name changes into 20170129 but does not change the order of the date.
It also does display the year for yy at all. I am a little bit lost. I know that I have to put this also into the replace function, but it won't work with the ways I tried.
Could you help me out?
import os, fnmatch
file_path = 'censored path'
files_to_rename = fnmatch.filter(os.listdir(file_path), '*.pdf')
for file_name in files_to_rename:
yy = file_name[-4:-1]
mm = file_name[-6:-4]
dd = file_name[-8:-6]
date=dd + '.' + mm + '.'+ yy
if fnmatch.fnmatch(file_name, '*2017*'):
new_name = os.rename(file_path + file_name, file_path + file_name.replace(file_name[0:17], ''))
new_name=str(new_name)+str(date)
print(new_name)