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
  Can rich.logging output to file? pyfoo 1 186 Mar-21-2024, 10:30 AM
Last Post: pyfoo
  file open "file not found error" shanoger 8 946 Dec-14-2023, 08:03 AM
Last Post: shanoger
  error "cannot identify image file" part way through running hatflyer 0 616 Nov-02-2023, 11:45 PM
Last Post: hatflyer
  Need to replace a string with a file (HTML file) tester_V 1 699 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 874 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  How to "tee" (=split) output to screen and into file? pstein 6 1,292 Jun-24-2023, 08:00 AM
Last Post: Gribouillis
  output provide the filename along with the input file processed. arjunaram 1 903 Apr-13-2023, 08:15 PM
Last Post: menator01
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,048 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,067 Dec-15-2022, 04:32 PM
Last Post: Larz60+
Photo Making Zip file of a file and Directory Nasir 2 985 Oct-07-2022, 02:01 PM
Last Post: Nasir

Forum Jump:

User Panel Messages

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