简体   繁体   中英

In Python, how to write strings in a file in a loop and before the loop stops, I can get the results runtimely?

In python, I need to record some results in a file. The results are generated by a function in a loop. The following code shows an example:

with open('result_file', 'w') as file:     
    for i in xrange(10000):
        result = somethingTakesTime()
        file.write(str(result), '\n')

Function somethingTakesTime() is time costly. I would like to check the result_file even the program is still working. However, with the current Python 2.7, I only can get the result after the for loop finish. Is there any method that I can see the result (in result_file) even the code is still working?

When programs write data to files, they usually keep the data in an internal buffer to prevent frequent disk writes (which can slow things down). But if the generation of the data is slower than the disk write would otherwise be, it can sometimes be useful to tell the program that you'd like to flush the data immediately to the file. To do this you'd use the .flush method of the file object.

Eg

with open('result_file', 'w') as file:     
    for i in xrange(10000):
        result = somethingTakesTime()
        file.write(str(result), '\n')
        file.flush()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM