Search code examples
pythonpython-2.7global-variablesglob

Unable to match the files


I am trying to append my csv files on a weekly basis. There are many files as per weeks but I only have to append the file as per a week such that I can publish the file on a weekly basis which will be a consolidated weekly CSV file.

the files are of this type in naming convention:

MidnightJob_Results_1-02-01-2019.csv

MidnightJob_Results_1-03-01-2019.csv

MidnightJob_Results_1-04-01-2019.csv

Above format is in Week-dd-mm-yyyy format. Result should be that it matches the files of the week and append them in a single file. However I am not able to make glob function understand how to achieve that. Please suggest

import datetime;
from glob import glob
current_week =(datetime.date.today().isocalendar()[1]-1)
file_name = "MidnightJob_Results_%d-" % (current_week)
print current_week
print file_name
with open('main.csv', 'a') as singleFile:
for csv in glob("file_name*.csv"):
    if csv == 'main.csv':
        pass
    else:
        for line in open(csv, 'r'):
            singleFile.write(line)

Solution

  • First thing you should format the file_name in glob function as shown below then you should open file in append mode because if file exist you dont want to delete the old file.

    import datetime;
    from glob import glob 
    current_week =(datetime.date.today().isocalendar()[1]-1)
    file_name = "MidnightJob_Results_%d-" % (current_week)
    print current_week
    print file_name
    with open('main.csv', 'a') as singleFile:
       for csv in glob("%s*.csv"%(file_name)):
            if csv == 'main.csv':
                pass
            else:
                for line in open(csv, 'a+'):
                     singleFile.write(line)