Python Forum
File name as part of output file name
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
File name as part of output file name
#1
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')
Reply
#2
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')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to write variable in a python file then import it in another python file? tatahuft 4 867 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 1,024 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  FileNotFoundError: [Errno 2] No such file or directory although the file exists Arnibandyo 0 824 Aug-12-2024, 09:11 AM
Last Post: Arnibandyo
  "[Errno 2] No such file or directory" (.py file) IbrahimBennani 13 6,154 Jun-17-2024, 12:26 AM
Last Post: AdamHensley
  Can rich.logging output to file? pyfoo 1 2,095 Mar-21-2024, 10:30 AM
Last Post: pyfoo
  file open "file not found error" shanoger 8 6,053 Dec-14-2023, 08:03 AM
Last Post: shanoger
  error "cannot identify image file" part way through running hatflyer 0 1,916 Nov-02-2023, 11:45 PM
Last Post: hatflyer
  Need to replace a string with a file (HTML file) tester_V 1 1,866 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  How can I change the uuid name of a file to his original file? MaddoxMB 2 2,076 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  How to "tee" (=split) output to screen and into file? pstein 6 3,965 Jun-24-2023, 08:00 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020