Python Forum

Full Version: File name as part of output file name
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am very new to Python, and have run into an issue. I am trying to output several different formats of GPS into different KML files. I have my original CSV filename I have to type in manually, but then I want it to spit out the CSV filename with "_fmt1", "_fmt2" so in the end it generates "filename_fmt1.kml", "filename_fmt2.kml" and so on.

Here is what I have.
with open('filename.csv', 'r') as csvfile:
    TM_Data = csv.reader(csvfile, delimiter=',')
    for column in TM_Data:
    time.append(str(column[0]))
    fmt1Lat.append(column[1]))
    fmt1Lon.append(column[2]))
    fmt2Lat.append(column[3]))
    fmt2Lon.append(column[4]))
    fmt3Lat.append(column[5]))
    fmt3Lon.append(column[6]))

kmlfmt1=simplekml.kml()
kmlfmt2=simplekml.kml()
kmlfmt3=simplekml.kml()
for I in range(len(time)-1):
    if i % 10 is 0:
        #a bunch of stuff that doesn't matter.

kmlfmt1.save('_fmt1.kml')
kmlfmt2.save('_fmt2.kml')
kmlfmt3.save('_fmt3.kml')
Save the filename as a variable. Then you can use it in multiple places:
filename = 'filename'
with open(filename + '.csv', 'r') as csvfile:
    TM_Data = csv.reader(csvfile, delimiter=',')
    for column in TM_Data:
    time.append(str(column[0]))
    fmt1Lat.append(column[1]))
    fmt1Lon.append(column[2]))
    fmt2Lat.append(column[3]))
    fmt2Lon.append(column[4]))
    fmt3Lat.append(column[5]))
    fmt3Lon.append(column[6]))
 
kmlfmt1=simplekml.kml()
kmlfmt2=simplekml.kml()
kmlfmt3=simplekml.kml()
for I in range(len(time)-1):
    if i % 10 is 0:
        #a bunch of stuff that doesn't matter.
 
kmlfmt1.save(filename + '_fmt1.kml')
kmlfmt2.save(filename + '_fmt2.kml')
kmlfmt3.save(filename + '_fmt3.kml')