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