[Tutor] Read, Write, Split and Append lines from a text file

Dave Angel davea at davea.name
Sun Jul 20 04:08:37 CEST 2014


LN A-go-go <lnartist at yahoo.com.dmarc.invalid> Wrote in message:


By trying to include an attachment,  you make it impossible for me
 to quote your actual message. Please use plain text message
 without attachments,  to maximize the number of people that can
 read and readily respond. 


> States	OJ
> AK	36
> AL	39
> AR	39
> AZ	45

5 lines should be enough to get the idea.

You don't specify the environment,  so I'll have to assume Python
 3.x. And it is likely Windows since you use x: as though it were
 part of the path.

>  You'll have to pretend this is properly quoted.

¥¥¥¥¥¥¥¥¥¥¥¥¥

 and now my newbie attempt:
# I can get this far in approach #1
>>> filename = "z:/Geog482/egund919/Program1/BOJ.txt"
>>> myfile = open(filename,"r")
>>> newfile = "z:/Geog482/egund919/Program1/BOJ_B.txt"
>>> mynewfile = open(newfile,"w")
>>> while True:
 line = myfile.readline()
 print
 line
 mynewfile.write(line)
 if not line: break
 
₩₩₩₩₩₩₩₩₩₩₩

print by itself is a no-op. If you had included the parentheses, 
 it'd at least produce a blank line.

line by itself is also a no-op. What were you trying to do with
 the variable? 

You neglected to close either of the files. Won't probably matter
 here, but it will someday.  Best get the habit now.

Just what was the point of this fragment?  In what way did it fall
 short, other than having two no-ops in it?

You have a different fragment with other apparent problems.  But
 you never spell out the problem. 

-- 
DaveA



More information about the Tutor mailing list