writing on file not until the end

Peter Otten __peter__ at web.de
Mon May 25 02:38:13 EDT 2009


Alexzive wrote:

> 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?

Do you check the contents of the resulting file by feeding it to another 
program? 

> 53, 0.170973146505, 0.0466686190136
> 57, 0.231749688431, 0.0405121944142
> t60, 0.250420691759, 0.0399644155193
> 58, 0.234883810317, 0.0488399925217
> 61, 0.2666025, 0.03541845
 
There's a "t" at the start of the line after the last line you claim to see, 
so maybe your input file is corrupt and your reader stumbles over that line.

Peter




More information about the Python-list mailing list