writing on file not until the end

Benjamin Kaplan bsk16 at case.edu
Sun May 24 16:15:15 EDT 2009


On Sun, May 24, 2009 at 2:46 PM, Alexzive <zasaconsulting at gmail.com> wrote:

> Hello,
> I am a newby with python. I wrote the following code to extract a text
> from a file and write it to another file:
>
> linestring = open(path, 'r').read() #read all the inp file in
> linestring
>
> i=linestring.index("*NODE")
> i=linestring.index("E",i)
> e=linestring.index("*",i+10)
> textN = linestring[i+2:e-1] # crop the ELement+nodes list
> Nfile = open("N.txt", "w")
> Nfile.write(textN)
>
> unfortunately when I check N.txt some lines are missing (it only crop
> until "57, 0.231749688431, 0.0405121944142" but I espect the final
> line to be "242, 0.2979675, 0.224605896461". I check textN and it has
> all the lines until "242..")
>
> when I try Nfile.write(textN) again it writes some more lines but
> still not all!
> what is wrong whit this?
>
> Many thanks for your help!
> Alex
>
> the original file to crop (in path) looks like this:


[snip file]

I didn't test this, but I would guess you're seeing this because of
buffering. Try adding Nfile.flush() to the end.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090524/3ec91d49/attachment.html>


More information about the Python-list mailing list