-1

This is a follow up to Using fieldinfo.setVisible in ArcPy?

I am taking an Intro to programming course and working on a project, this will be part of a larger script.

The code runs and creates a new shapefile, but the fields I am trying to hide are not hidden on export.

Any resources for additional information that would help as well?

import arcpy
arcpy.env.overwriteOutput = True
arcpy.env.workspace = "C:\GIS\python"
arcpy.env.overwriteOutput = True

x = "New" + ".shp"
y = "New.lyr"

arcpy.MakeFeatureLayer_management(x, "Temp")
arcpy.SaveToLayerFile_management("Temp", y, "ABSOLUTE")
arcpy.AddMessage("Layer File Successfully Created")

# List of fields to hide
# desc.OIDFieldName is the name of the 'FID' field

z = "New" + ".lyr"
a ="Final.lyr"
g = "C:\GIS\python"
desc = arcpy.Describe(z)
field_info = desc.fieldInfo
fieldsToHide = [desc.OIDFieldName, 'APN', 'Situs']

for i in range(0, field_info.count):
    if field_info.getFieldName(i) in fieldsToHide:
        field_info.setVisible(i, "HIDDEN")

arcpy.MakeFeatureLayer_management(z, "New_Final", "", "", field_info)
arcpy.FeatureClassToShapefile_conversion("New_Final",g)
arcpy.AddMessage("Fields Hidden")
3
  • 2
    Note: there is no way to delete the FID field, it is a required field. – user2856 Feb 28 '17 at 0:57
  • Thank you, are shape & FID the only required fields than? – user92389 Feb 28 '17 at 1:36
  • 2
    Shapefiles must have at least one field other than FID and shape. This is a limitation of the shapefile format as the FID and shape fields are not actually stored in the .dbf file which has to have something in it. This does not apply to other formats, such as a GeoDatabase feature class – user2856 Feb 28 '17 at 2:52
3

I have run your script successfully and it outputs a new shapefile with the fields removed. Note that it will not remove a FID field as this is a required field in a shapefile.

I have cleaned out some unnecessary code

import arcpy
arcpy.env.overwriteOutput = True
arcpy.env.workspace = r"C:\GIS\python"

x = "New.shp"
xFeatLyr = "Temp"
arcpy.MakeFeatureLayer_management(x, xFeatLyr)
desc = arcpy.Describe(xFeatLyr)

field_info = desc.fieldInfo
fieldsToHide = ['APN', 'Situs']

for i in range(0, field_info.count):
    if field_info.getFieldName(i) in fieldsToHide:
        field_info.setVisible(i, "HIDDEN")

arcpy.MakeFeatureLayer_management(xFeatLyr, "New_Final", "", "", field_info)
arcpy.FeatureClassToShapefile_conversion("New_Final",arcpy.env.workspace)
1
  • Thank you Midavalo. I learned I can export to the workspace as well., I have alot to learn – user92389 Feb 28 '17 at 1:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.