Pre-process the file list to sort it:
- Create a list of file_names,
- extract the relevant info from the name and create a datetime object,
- sort on the datetime object,
- then use the sorted list.
import operator
fyles = ['CB02 May 2014.dailysum',
'CB01 Apr 2015.dailysum',
'CB01 Jul 2015.dailysum',
'CB01 May 2015.dailysum',
'CB01 Sep 2015.dailysum',
'CB01 Oct 2015.dailysum',
'CB13 May 2015.dailysum',
'CB01 Jun 2017.dailysum',
'CB01 Aug 2015.dailysum'
]
new_fyles = []
for entry in fyles:
day, month, year = entry.split()
year, _ = year.split('.')
day = day[-2:]
## print(entry, (month, year))
dt = datetime.datetime.strptime(' '.join((day, month, year)), '%d %b %Y')
## print(entry, dt)
new_fyles.append((entry, dt))
date = operator.itemgetter(1)
f_name = operator.itemgetter(0)
new_fyles.sort(key = date)
for entry in new_fyles:
print(f_name(entry))
You can make the file list like this:
import os, os.path
fyles = [fn for fn in os.listdir(inputdirectory) if fn.endswith('.dailysum')]
Then, after sorting, write the contents of each file to the new file:
with open('totalsum.csv', 'w') as out:
for entry in new_fyles:
f_path = os.path.join(inputdirectory, f_name(entry))
with open(f_path) as f:
out.write(f.read())
You could perform the sorting in a function
date = operator.itemgetter(1)
f_name = operator.itemgetter(0)
def f_name_sort(f_list):
'''Return sorted list of file names'''
new_fyles = []
for entry in f_list:
day, month, year = entry.split()
year, _ = year.split('.')
day = day[-2:]
dt = datetime.datetime.strptime(' '.join((day, month, year)), '%d %b %Y')
new_fyles.append((entry, dt))
new_fyles.sort(key = date)
return [f_name(entry) for f_name in new_fyles]
and use it like this:
for entry in f_name_sort(fyles):
...
Or write a function that converts a filename to a datetime object and use it as the key for sorting
def key(f_name):
day, month, year = f_name.split()
year, _ = year.split('.')
day = day[-2:]
return datetime.datetime.strptime(' '.join((day, month, year)), '%d %b %Y')
fyles.sort(key = key)
for entry in fyles:
...