Python Forum
Trying to figure how to continue my numbering for my "file 1:"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to figure how to continue my numbering for my "file 1:"
#1
def main():

import os
import arcpy
arcpy.env.workspace = "I:\\GEO5419_Python\\Lab2"
path = r"I:\GEO5419_Python\Lab2"
fcList = arcpy.ListFeatureClasses()
file = open(path + r'\listfeatures.txt', "w+") # writes new text
for line in fcList:
file.write("File 1:" + line)
file.write("\n") #writes next feature on seperate line

file.close()

if __name__ == '__main__':
main()
Reply
#2
What is your question?
And please pust your code in Python code tags, you can find help here.
Reply
#3
The question I have is that I am trying to figure how to continue my numbering for my "file 1:" to continue the numbering but I keep getting File 1:

def main():

    import os
    import arcpy
    arcpy.env.workspace = "I:\\GEO5419_Python\\Lab2"
    path = r"I:\GEO5419_Python\Lab2"
    fcList = arcpy.ListFeatureClasses()
    file = open(path + r'\listfeatures.txt', "w+") # writes new text
    for line in fcList:
        file.write("File 1:" + line)
        file.write("\n") #writes next feature on seperate line
        file.close()

if __name__ == '__main__':
    main()
Reply
#4
imports are best placed on top of the file.
In line #9 you open file (only time in the code), and then in line #12 you close the file on every for iteration. Anyway, using context manager is a better to handle files. It will close the file automatically after you are done with the processing. Try something like this:

import os
import arcpy

def main():

    arcpy.env.workspace = "I:\\GEO5419_Python\\Lab2"
    path = r"I:\GEO5419_Python\Lab2"
    fcList = arcpy.ListFeatureClasses()
    with open(path + r'\listfeatures.txt', "w+") as file:
        for line in fcList:
            file.write("File 1:" + line)
            file.write("\n")  # writes next feature on seperate line

if __name__ == '__main__':
    main()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can't seem to figure out how to delete several lines from a text file Cosmosso 9 4,029 Dec-10-2019, 11:09 PM
Last Post: Cosmosso

Forum Jump:

User Panel Messages

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