Looping Problem (Generating files - only the last record generates a file)

John Abel jabel at plus.net
Wed Oct 26 05:15:15 EDT 2005


vasilijepetkovic at yahoo.com wrote:

>Hello All,
>
>I have a problem with the program that should generate x number of txt
>files (x is the number of records in the file datafile.txt).
>
>Once I execute the program (see below) only one file (instead of x
>files) is created. The file created is based on the last record in
>datafile.txt.
>
>The program is as follows:
>====================================
>#!  python
>
>HEADER = "This page displays longitude-latitude information"
>SUBHEADER = "City"
>
>for line in open("datafile.txt"):
>
>
>    town, latlong = line.split('\t')
>
>f = open(town + ".txt", "w+")
>
>f.write(HEADER + "\n")
>f.write(SUBHEADER + ": " + town + "\n")
>f.write("LAT/LONG" + ": " + latlong + "\n")
>f.close()
>
>  
>
These lines need to be within your loop.

J



More information about the Python-list mailing list