Python Forum
Trying to figure how to continue my numbering for my "file 1:" - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Trying to figure how to continue my numbering for my "file 1:" (/thread-8002.html)



Trying to figure how to continue my numbering for my "file 1:" - chavez814 - Feb-02-2018

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()


RE: Trying to figure how to continue my numbering for my "file 1:" - j.crater - Feb-02-2018

What is your question?
And please pust your code in Python code tags, you can find help here.


RE: Trying to figure how to continue my numbering for my "file 1:" - chavez814 - Feb-02-2018

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()



RE: Trying to figure how to continue my numbering for my "file 1:" - j.crater - Feb-03-2018

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()