Home:ALL Converter>python endless loop stops writing to file

python endless loop stops writing to file

Ask Time:2018-01-26T20:19:08         Author:Zdenko Brzic

Json Formatter

I am downloading dynamic data from api server and writing it to file by means of an endless loop in python. For whatever reason the program stops writing to file after couple thousand lines, while the program seems to be running. I am not sure where the problem is. The program does not give error, ie. it is not that the API server is refusing response. When restarted it continues as planned. I am using python 3.6 and win10.

Simplified version of the code looks something like:

import requests, json, time
while True:
    try:
        r = requests.get('https://someapiserver.com/data/')
        line = r.json()

        with open('file.txt', 'a') as f:
            f.write(line)
        time.sleep(5)
    except:
        print('error')
        time.sleep(10)

Author:Zdenko Brzic,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/48461240/python-endless-loop-stops-writing-to-file
yy