Search code examples
pythontimecanopy

Python time stamp does not print - open large txt


Code below open and loops over a 500 MB txt file. It's running for almost half hour now and the funny thing is it did not print the first 'time.gmtime()'. I restarted the Kernel a couple of times to check for problems but all seems fine.

Why didn't python print my initial 'time.gmtime()'?

print time.gmtime()

with open(FullPath) as FileObj:
    for lines in FileObj:
        i +=1
print i

print time.gmtime()

EDIT:

Just ran exact same code over a much smaller file (1523KB) and here's what I got:

time.struct_time(tm_year=2015, tm_mon=4, tm_mday=27, tm_hour=14, tm_min=11, tm_sec=28, tm_wday=0, tm_yday=117, tm_isdst=0)
2852
time.struct_time(tm_year=2015, tm_mon=4, tm_mday=27, tm_hour=14, tm_min=11, tm_sec=41, tm_wday=0, tm_yday=117, tm_isdst=0)

DETAIL: BOTH TIME STAMPS WHERE PRINTED ONLY WHEN CODE WAS DONE!


Solution

  • You need to import sys;sys.stdout.flush() after your first print as canopy buffers stdout by default.