Home > OS >  Saving results in the new file everytime I rerun the python code (the name should include the date t
Saving results in the new file everytime I rerun the python code (the name should include the date t

Time:08-19

few parts of the code:

def daysList(adate) :

    from datetime import date, timedelta

    from_y = int(adate[0][0:4])
    from_m = int(adate[0][4:6])
    from_d = int(adate[0][6:])

    to_y  = int(adate[1][0:4])
    to_m  = int(adate[1][4:6])
    to_d  = int(adate[1][6:])

    to_date = date(to_y,to_m,to_d)
    from_date = date(from_y,from_m,from_d)

    days = (to_date-from_date).days

    dateList = []
    for d in range(0,days 1) :
        n_date = str(from_date   timedelta(days=d))
        dateList.append(n_date.split("-"))

    return dateList

outfile = f'Stations{daysList}.csv'

with open(options.outfile, 'w') as ofile:
    for row in DB:
        ofile.write('%s\n' % ','.join(row))

The error that I am getting:

OSError: [Errno 22] Invalid argument: 'Stations<function daysList at 0x000001F4E4BBC430>.csv'

CodePudding user response:

Note that in this line:

outfile = f'Stations{daysList}.csv'

You're not using the output of daysList for some argument; you're using daysList itself, the function. The code you've provided is incomplete (What is options? What do you intend to pass as an argument to daysList?) but I'd start there.

CodePudding user response:

When you open (create) a file you must pass a string as filename. However, the variable outfile that contains such variable is not correctly formed because dayslist does not seems to be directly printable. I suggest you to write it this way:

days = daysList(adate)
start = days[0]
end = days[-1]
outfile = f'Stations{start}-{end}.csv'

If you prefer you can write it inline:

outfile = f'Stations{days[0]}-{days[-1]}.csv'
  • Related