user3235542
user3235542

Reputation:

Out file using the time during writing of file

I want to write the out file using the time at the writing of the file as the filename.

import time, datetime
current_time = time.time()
endtime = current_time + 12*60*60
while current_time < endtime:    

    file_time = time.strftime('%Y-%m-%d_%H:%M:%S')

    outfile = open ('outfile_{}.txt'.format(file_time),'w')
    outfile.close()
    time.sleep(30)

However there is a problem with the file_time. How to define that time?

From comment:

thanks for your efforts, but still i get the following error:IOError: [Errno 22] invalid mode ('w') or filename: 'outfile_2014-03-02_16:36:13.txt'

Upvotes: 0

Views: 51

Answers (1)

Vin.AI
Vin.AI

Reputation: 2437

Also assign current_time while looping:

# ...
file_time = time.strftime('%Y-%m-%d_%H %M %S')
outfile = open ('outfile_%s.txt' % (file_time),'w')
outfile.close()
time.sleep(30)
current_time = time.time()

Upvotes: 2

Related Questions