Select to view content in your preferred language

Save field names to a .txt file

1843
3
Jump to solution
11-26-2019 05:22 AM
SoratoSouza_e_Silva
Frequent Contributor

Hi,

how can i change this code to write to a file all field names of each feature class that exists in a geodatabase.

import arcpy
enviro = r"C:/EsriTraining/PYTH/Automate/"
arcpy.env.workspace = enviro +"{}".format("SanDiego.gdb")
field_list = arcpy.ListFields("MajorAttractions")
txtFile = open(enviro +"{}".format("Majorattractions.txt"),"w")
txtFile.write("MajorAttractions field information" + "\n")
txtFile.write("-------------------------------------" + "\n")
for field in field_list:
      line = "Name: {}, Type: {}, Length: {}\n".format(
            field.name, field.type, field.length)
      txtFile.write(line)
txtFile.close()

Thank´s

0 Kudos
1 Solution

Accepted Solutions
deleted-user-NvcfpBOWaKwr
Deactivated User

Instead of trying to fix your code I just wrote this up. I tested it several times to make sure it works. The script will make a folder called "Logfiles" in the same directory path that the script is running from. There you will find the text file with the field information for all of the feature classes within the .gdb

Happy Thanksgiving, Enjoy and most of all enjoy the time ahead with the family!!

EDIT:

Added in some code to write the field type and length to the logfile. I missed that part when I grazed over your code.

You can also replace my concatenation's with the format() function. Especially on line 38. Bad habit on my end. 

import arcpy
import os
from os import path

#################################################################
#
# Change the workSpace variable to point to the .gdb you want to work with
#
##################################################################

workSpace = r"Put the path of your .gdb here.. Make sure you keep the path within the double quotes and don't delete the r" # Sorry if you already know this.
logFile =  os.path.dirname(os.path.abspath(__file__)) + "\\" + "Logfiles" + "\\" + "Field_Export_Log.txt"
logFolder = os.path.dirname(os.path.abspath(__file__)) + "\\" + "Logfiles"
arcpy.env.workspace = workSpace

if path.exists(logFolder): # Checks to see if there is a Logfiles folder in the same directory path that the script is running from
    pass

else: # If there is not a Logfiles folder it will make one
    os.mkdir(logFolder)

if path.exists(logFile): # This checks to see if there is already a logfile. If there is one it will pass over this section
    pass

else:
    txtWriter = open(logFile, "a")  # If there is not a logfile it will create one
    txtWriter.write("******Begin Field Name Listing******")
    txtWriter.close()

for listFC in arcpy.ListFeatureClasses(): # This creates a list of the featureclasses within the .gdb and loops through each one
    describe = arcpy.Describe(listFC) # This pull information about the featureclass so we can use the .name method in the txt file
    txtWriter2 = open(logFile, "a")  # Opens the lofile
    txtWriter2.write("\n\n\n********" + " Field Names for Feature Class " + describe.name + " ********") # Writes some stuff to the log file

    for listFields in arcpy.ListFields(listFC): # This creates a list of the fields in the feature class that the loop on line 23 is currently on
        txtWriter2.write("\n\nName: " + listFields.name + "\nField Type: " + listFields.type + "\nField Length: " + str(listFields.length)) # This writes the field names, types and lengths to the logfile
        print("Added Field " + listFields.name + " From Feature Class: " + describe.name + " To .txt file") # For debugging so you know what the script is doing

    txtWriter2.close # This closes the write session on the logfile

print("Done!!!") # All done
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

3 Replies
deleted-user-NvcfpBOWaKwr
Deactivated User

Instead of trying to fix your code I just wrote this up. I tested it several times to make sure it works. The script will make a folder called "Logfiles" in the same directory path that the script is running from. There you will find the text file with the field information for all of the feature classes within the .gdb

Happy Thanksgiving, Enjoy and most of all enjoy the time ahead with the family!!

EDIT:

Added in some code to write the field type and length to the logfile. I missed that part when I grazed over your code.

You can also replace my concatenation's with the format() function. Especially on line 38. Bad habit on my end. 

import arcpy
import os
from os import path

#################################################################
#
# Change the workSpace variable to point to the .gdb you want to work with
#
##################################################################

workSpace = r"Put the path of your .gdb here.. Make sure you keep the path within the double quotes and don't delete the r" # Sorry if you already know this.
logFile =  os.path.dirname(os.path.abspath(__file__)) + "\\" + "Logfiles" + "\\" + "Field_Export_Log.txt"
logFolder = os.path.dirname(os.path.abspath(__file__)) + "\\" + "Logfiles"
arcpy.env.workspace = workSpace

if path.exists(logFolder): # Checks to see if there is a Logfiles folder in the same directory path that the script is running from
    pass

else: # If there is not a Logfiles folder it will make one
    os.mkdir(logFolder)

if path.exists(logFile): # This checks to see if there is already a logfile. If there is one it will pass over this section
    pass

else:
    txtWriter = open(logFile, "a")  # If there is not a logfile it will create one
    txtWriter.write("******Begin Field Name Listing******")
    txtWriter.close()

for listFC in arcpy.ListFeatureClasses(): # This creates a list of the featureclasses within the .gdb and loops through each one
    describe = arcpy.Describe(listFC) # This pull information about the featureclass so we can use the .name method in the txt file
    txtWriter2 = open(logFile, "a")  # Opens the lofile
    txtWriter2.write("\n\n\n********" + " Field Names for Feature Class " + describe.name + " ********") # Writes some stuff to the log file

    for listFields in arcpy.ListFields(listFC): # This creates a list of the fields in the feature class that the loop on line 23 is currently on
        txtWriter2.write("\n\nName: " + listFields.name + "\nField Type: " + listFields.type + "\nField Length: " + str(listFields.length)) # This writes the field names, types and lengths to the logfile
        print("Added Field " + listFields.name + " From Feature Class: " + describe.name + " To .txt file") # For debugging so you know what the script is doing

    txtWriter2.close # This closes the write session on the logfile

print("Done!!!") # All done
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
deleted-user-NvcfpBOWaKwr
Deactivated User

So to help you learn how I wrote the above code here are some good links to reading material. 

Creating and working with directories in python:

https://stackabuse.com/creating-and-deleting-directories-with-python/ 

Reading and writing to text files with python:

Reading and Writing to text files in Python - GeeksforGeeks 

ArcPy List Featureclasses:

ListFeatureClasses—ArcPy Functions | ArcGIS Desktop 

ArcPy List Fields:

ListFields—ArcPy Functions | ArcGIS Desktop 

ArcPy Field Object:

Field—ArcPy classes | ArcGIS Desktop 

ArcPy Describe Function:

Describe—ArcPy Functions | ArcGIS Desktop 

Hope this helps

SoratoSouza_e_Silva
Frequent Contributor

Thank you very much

0 Kudos