I'm trying to write datetime.datetime.now()
to a textfile after each iteration of a for loop, so I can calculate the number of calls made per second to our API. Here's my working code
import requests
import datetime
import config
# create a file with a timestamp starting at runtime.
with open('timelog-' + str(datetime.datetime.now().strftime("%y-%m-%d--%H-%S")) + '.txt', 'a') as log_time:
for x in range(10):
req = requests.post('https://' + config.env.lower() + '.website.com/args'
+ config.cli + '/args/'
+ config.con + '/args/?args='
+ config.cel + '&date='
+ config.req + '&args', headers=config.head)
log_time.write(str(datetime.datetime.now().replace(microsecond=0)) + "\n")
log_time.flush()
Now, the confusing thing to me, if I were to comment out req
, I would not need to include log_time.flush()
. Likewise, if I were to remove log_time.flush()
, log_time.write()
would not function correctly, instead I'd end with a blank file.
Is there any particular reason for this behavior?