2

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!

BuckTurgidson
  • 289
  • 3
  • 8
  • 17
  • "I restarted the Kernel" What kernel? –  Apr 27 '15 at 13:55
  • Buck, nothing seems off about the code that you posted. Are you sure the stdout isn't being captured by another process? Is the last `time.gmtime()` printing? – Daniel Timberlake Apr 27 '15 at 13:55
  • @Tichodroma. In Canopy: Run/Restard Kernel. Its just a way to break the code. – BuckTurgidson Apr 27 '15 at 13:57
  • @TimberlakeCoding. Haven't seen the end of the code yet! And don't wanna leave it running for hours and then figure sometime basic wrong. But if I comment the main loop the code run fine! – BuckTurgidson Apr 27 '15 at 14:00
  • Maybe the script is freezing due to lack of RAM on your system ? try using `with open(FullPath, 1000) as FileObj:` – ZdaR Apr 27 '15 at 14:02
  • @anmol_uppal. Task manager shows me using 2.93 GB and things seem to be running. In any case, should't the 'time.gmtime()' be printed before it freezes? Will try anyways... – BuckTurgidson Apr 27 '15 at 14:05
  • Yeah but this depends upon the Editor you are using, For example in canopy and Idle I am able to print very large amount of data and doing the same in Sublime, Freezes the system and I have to Force quit the application, – ZdaR Apr 27 '15 at 14:09
  • So you got your answer, The file size is doing all the magic here. – ZdaR Apr 27 '15 at 14:14

1 Answers1

4

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

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321