writing on file not until the end
davea at ieee.org
Mon May 25 16:06:17 CEST 2009
> I closed and restarted the python console. Now this code (with added
> "Nfile.close()" at the end) seems to work properly:
> linestring = open(path, 'r').read()
> textN = linestring[i+2:e-1]
> Nfile = open("N.txt", "w")
> thanks, Alex
Others had already mentioned close(), but I didn't bother, since it
would be automatically closed when you exited the function, or finished
the script. I never dreamed you were running all this from the
interpreter. If so, why didn't you copy the prompts?
The close is best done explicitly, but it is implicit when the Nfile
goes out of scope, or gets reassigned (like the new open).
However, the bug I described is still there. Study the following, and
buf = "abcdefghijklmnopqrstg000"
i = buf.index("d")
e = buf.index("g", i+10)
buf2 = buf[i+2:e-1]
running it produces the string:
Notice the 't' is missing. If you're deliberately doing that, fine.
But I doubt it.
More information about the Python-list