Identify and delete all unrequired fields from Shape file using Arcpy












2















I have some Shape files inside one Folder and for the sake of datacompression I want to delete all fields from Table of Content that can be removed (that are not required)!



What I have:





What I expect:



enter image description here



My code that is not working:



import arcpy
import os

arcpy.env.workspace = r"C:Temp"
fcList = arcpy.ListFeatureClasses()

for fc in fcList:
fields = arcpy.ListFields(fc)
for field in fields:
delFlds = [field for field in fields if field != "FID" or field != "Shape"]
arcpy.DeleteField_management(fc, delFlds)


I am using python 2.7 and ArcMap 10.4.1










share|improve this question









New contributor




gisgis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    2















    I have some Shape files inside one Folder and for the sake of datacompression I want to delete all fields from Table of Content that can be removed (that are not required)!



    What I have:





    What I expect:



    enter image description here



    My code that is not working:



    import arcpy
    import os

    arcpy.env.workspace = r"C:Temp"
    fcList = arcpy.ListFeatureClasses()

    for fc in fcList:
    fields = arcpy.ListFields(fc)
    for field in fields:
    delFlds = [field for field in fields if field != "FID" or field != "Shape"]
    arcpy.DeleteField_management(fc, delFlds)


    I am using python 2.7 and ArcMap 10.4.1










    share|improve this question









    New contributor




    gisgis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      2












      2








      2








      I have some Shape files inside one Folder and for the sake of datacompression I want to delete all fields from Table of Content that can be removed (that are not required)!



      What I have:





      What I expect:



      enter image description here



      My code that is not working:



      import arcpy
      import os

      arcpy.env.workspace = r"C:Temp"
      fcList = arcpy.ListFeatureClasses()

      for fc in fcList:
      fields = arcpy.ListFields(fc)
      for field in fields:
      delFlds = [field for field in fields if field != "FID" or field != "Shape"]
      arcpy.DeleteField_management(fc, delFlds)


      I am using python 2.7 and ArcMap 10.4.1










      share|improve this question









      New contributor




      gisgis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      I have some Shape files inside one Folder and for the sake of datacompression I want to delete all fields from Table of Content that can be removed (that are not required)!



      What I have:





      What I expect:



      enter image description here



      My code that is not working:



      import arcpy
      import os

      arcpy.env.workspace = r"C:Temp"
      fcList = arcpy.ListFeatureClasses()

      for fc in fcList:
      fields = arcpy.ListFields(fc)
      for field in fields:
      delFlds = [field for field in fields if field != "FID" or field != "Shape"]
      arcpy.DeleteField_management(fc, delFlds)


      I am using python 2.7 and ArcMap 10.4.1







      arcpy shapefile arcgis-10.4






      share|improve this question









      New contributor




      gisgis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      gisgis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 8 hours ago









      BERA

      15.7k52042




      15.7k52042






      New contributor




      gisgis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 8 hours ago









      gisgisgisgis

      133




      133




      New contributor




      gisgis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      gisgis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      gisgis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          1 Answer
          1






          active

          oldest

          votes


















          3














          You are listing field objects with ListFields, not the field names which should be input to DeleteField. So change:



          [field for field in fields if field != "FID" or field != "Shape"]


          to:



          [field.name for field in fields if field.name not in ("FID","Shape")]


          But it is probably better to use the required property since object id and shape fields can have different names in different feature classes:



          for fc in fcList:
          fields_to_delete = [field.name for field in arcpy.ListFields(fc) if not field.required]
          fields_to_delete.pop() #Keep one non-required field
          for field in fields_to_delete:
          arcpy.DeleteField_management(fc, field)





          share|improve this answer


























          • Hi BERA thanks for your very quick replay! However it comes an Error saying: "Feature Classes and Tables must have at least one field other than OID and Shape."

            – gisgis
            8 hours ago











          • @gisgis ok you need to keep one then. I edited my answer.

            – BERA
            8 hours ago






          • 1





            perfect it worked! Thanks a lot.

            – gisgis
            8 hours ago











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "79"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          gisgis is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f311490%2fidentify-and-delete-all-unrequired-fields-from-shape-file-using-arcpy%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          You are listing field objects with ListFields, not the field names which should be input to DeleteField. So change:



          [field for field in fields if field != "FID" or field != "Shape"]


          to:



          [field.name for field in fields if field.name not in ("FID","Shape")]


          But it is probably better to use the required property since object id and shape fields can have different names in different feature classes:



          for fc in fcList:
          fields_to_delete = [field.name for field in arcpy.ListFields(fc) if not field.required]
          fields_to_delete.pop() #Keep one non-required field
          for field in fields_to_delete:
          arcpy.DeleteField_management(fc, field)





          share|improve this answer


























          • Hi BERA thanks for your very quick replay! However it comes an Error saying: "Feature Classes and Tables must have at least one field other than OID and Shape."

            – gisgis
            8 hours ago











          • @gisgis ok you need to keep one then. I edited my answer.

            – BERA
            8 hours ago






          • 1





            perfect it worked! Thanks a lot.

            – gisgis
            8 hours ago
















          3














          You are listing field objects with ListFields, not the field names which should be input to DeleteField. So change:



          [field for field in fields if field != "FID" or field != "Shape"]


          to:



          [field.name for field in fields if field.name not in ("FID","Shape")]


          But it is probably better to use the required property since object id and shape fields can have different names in different feature classes:



          for fc in fcList:
          fields_to_delete = [field.name for field in arcpy.ListFields(fc) if not field.required]
          fields_to_delete.pop() #Keep one non-required field
          for field in fields_to_delete:
          arcpy.DeleteField_management(fc, field)





          share|improve this answer


























          • Hi BERA thanks for your very quick replay! However it comes an Error saying: "Feature Classes and Tables must have at least one field other than OID and Shape."

            – gisgis
            8 hours ago











          • @gisgis ok you need to keep one then. I edited my answer.

            – BERA
            8 hours ago






          • 1





            perfect it worked! Thanks a lot.

            – gisgis
            8 hours ago














          3












          3








          3







          You are listing field objects with ListFields, not the field names which should be input to DeleteField. So change:



          [field for field in fields if field != "FID" or field != "Shape"]


          to:



          [field.name for field in fields if field.name not in ("FID","Shape")]


          But it is probably better to use the required property since object id and shape fields can have different names in different feature classes:



          for fc in fcList:
          fields_to_delete = [field.name for field in arcpy.ListFields(fc) if not field.required]
          fields_to_delete.pop() #Keep one non-required field
          for field in fields_to_delete:
          arcpy.DeleteField_management(fc, field)





          share|improve this answer















          You are listing field objects with ListFields, not the field names which should be input to DeleteField. So change:



          [field for field in fields if field != "FID" or field != "Shape"]


          to:



          [field.name for field in fields if field.name not in ("FID","Shape")]


          But it is probably better to use the required property since object id and shape fields can have different names in different feature classes:



          for fc in fcList:
          fields_to_delete = [field.name for field in arcpy.ListFields(fc) if not field.required]
          fields_to_delete.pop() #Keep one non-required field
          for field in fields_to_delete:
          arcpy.DeleteField_management(fc, field)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 1 hour ago

























          answered 8 hours ago









          BERABERA

          15.7k52042




          15.7k52042













          • Hi BERA thanks for your very quick replay! However it comes an Error saying: "Feature Classes and Tables must have at least one field other than OID and Shape."

            – gisgis
            8 hours ago











          • @gisgis ok you need to keep one then. I edited my answer.

            – BERA
            8 hours ago






          • 1





            perfect it worked! Thanks a lot.

            – gisgis
            8 hours ago



















          • Hi BERA thanks for your very quick replay! However it comes an Error saying: "Feature Classes and Tables must have at least one field other than OID and Shape."

            – gisgis
            8 hours ago











          • @gisgis ok you need to keep one then. I edited my answer.

            – BERA
            8 hours ago






          • 1





            perfect it worked! Thanks a lot.

            – gisgis
            8 hours ago

















          Hi BERA thanks for your very quick replay! However it comes an Error saying: "Feature Classes and Tables must have at least one field other than OID and Shape."

          – gisgis
          8 hours ago





          Hi BERA thanks for your very quick replay! However it comes an Error saying: "Feature Classes and Tables must have at least one field other than OID and Shape."

          – gisgis
          8 hours ago













          @gisgis ok you need to keep one then. I edited my answer.

          – BERA
          8 hours ago





          @gisgis ok you need to keep one then. I edited my answer.

          – BERA
          8 hours ago




          1




          1





          perfect it worked! Thanks a lot.

          – gisgis
          8 hours ago





          perfect it worked! Thanks a lot.

          – gisgis
          8 hours ago










          gisgis is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          gisgis is a new contributor. Be nice, and check out our Code of Conduct.













          gisgis is a new contributor. Be nice, and check out our Code of Conduct.












          gisgis is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Geographic Information Systems Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f311490%2fidentify-and-delete-all-unrequired-fields-from-shape-file-using-arcpy%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to label and detect the document text images

          Vallis Paradisi

          Tabula Rosettana