import arcpy from arcpy import env import os import time def main(): try: import arcpy, sys, traceback, os, glob, shutil arcpy.env.overwriteOutput = True log = r'Q:\1-EMPLOYEE INBOX\David\downloads\logSurveyData.txt' masterFolder = r"Q:\GIS\Field_Data\MT" outputFolder = r"C:\tmp\Shp_merged" dst = r'Q:\GIS\Field_Data\z_archive\PMM' #shutil.copytree(masterFolder, dst + time.strftime('%m_%d_%y')) #collect a list of subfolders in master folder arcpy.env.workspace = masterFolder arcpy.ListWorkspaces('','Folder') subfolderLst = arcpy.ListWorkspaces('','Folder') print subfolderLst for subfolder in subfolderLst: arcpy.env.workspace = subfolder fcLst = arcpy.ListFeatureClasses() for fc in fcLst: #file = fc in fcLst filename = "'" + arcpy.env.workspace + os.sep + fc + "'" print filename arcpy.AddField_management(fc, 'shpname','text') arcpy.CalculateField_management(fc, 'shpname', filename, "PYTHON" ) except: print arcpy.GetMessages() # Get the traceback object '"' + wildcard + '"' tb = sys.exc_info()[2] tbinfo = traceback.format_tb(tb)[0] # Concatenate information together concerning the error into a # message string pymsg = tbinfo + "\n" + str(sys.exc_type)+ ": " + str(sys.exc_value) # Return python error messages for use with a script tool arcpy.AddError(pymsg) # Print Python error messages for use in Python/PythonWin print pymsg if __name__ == '__main__': main()
filename = "'" + arcpy.env.workspace + os.sep + fc + "'"
Hi David,Add single quotes to your 'filename' variable. Try the following:for fc in fcLst: filename = "'" + os.path.abspath(__file__) + "'" arcpy.AddField_management(fc, 'shpname','text') arcpy.CalculateField_management(fc, 'shpname',filename, "PYTHON" )
for fc in fcLst: filename = "'" + os.path.abspath(__file__) + "'" arcpy.AddField_management(fc, 'shpname','text') arcpy.CalculateField_management(fc, 'shpname',filename, "PYTHON" )
Why do you use __file__ infilename = os.path.abspath(__file__) ?Why not filename = os.path.abspath(fc)I also assume that the filename variable needs to be properly quoted before being passed to CalculateFieldarcpy.CalculateField_management(fc, 'shpname', r'"%s"' % (filename), "PYTHON" )- that is, with double quotes (required by calculate field) surrounded by single quotes (needed to pass the double quotes as part of the string)
Los miembros registrados pueden publicar, seguir actualizaciones y más. ¿Nuevo aquí? Regístrate gratis.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.