[Tutor] (no subject)

Wolfgang Maier wolfgang.maier at biologie.uni-freiburg.de
Sun Jul 20 21:49:41 CEST 2014


On 20.07.2014 17:40, LN A-go-go wrote:
>
>  >>> filename = "C:/Python27/egund919/Program1/BOJ.txt"
>  >>> myfile = open(filename,"r")
>  >>> newfile = "C:/Python27/egund919/Program1/BOJ_B.txt"
>  >>> mynewfile = open(newfile,"w")
>  >>> while True:
>   line = myfile.readline()
>   print line
>   mynewfile.write(line)
>   if not line: break
>
> States OJ
> AK 36
> AL 39
> AR 39
> AZ 45
> CA 61

..

>
>  >>> myfile.close()

Closing the file you've read from is good, but not nearly as important 
as closing the file you've been writing to.

>  >>> infile = open('C:/Python27/egund919/Program1/BOJ_B.txt','r')

There's your problem ! You didn't close mynewfile before opening the 
file again for reading. Most likely, what you thought you've written to 
the file is still buffered in memory. The main purpose of closing a file 
after writing is to flush the contents of this buffer to disk.
Try mynewfile.close() just before infile = open ..

Best,
Wolfgang



More information about the Tutor mailing list