calculating the complete path to an attribute

1256
8
01-04-2013 06:42 AM
davidmetzler
New Contributor II
Greetings,
    I have a question that I believe has a simple answer but I just can't quite find it. I am trying to calculate the absolute path of a shapefile to an attribute. so the goal is to have a "shpname" field that has the comple path e.g. C:/desktop/superfunline.shp. here is my python.

def main():
    try:
        import arcpy, sys, traceback, os, glob
        arcpy.env.overwriteOutput = True
        masterFolder = r"Q:\GIS\Field_Data\MT"
        outputFolder = r"C:\tmp\Shp_merged"

        #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 = os.path.abspath(__file__)
                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()



I am thinking the bold is where the error is occuring. the error is:

ERROR 000539: <type 'exceptions.SyntaxError'>: invalid syntax (<expression>, line 1)


Thanks!

Dave
Tags (2)
0 Kudos
8 Replies
markdenil
Occasional Contributor III
Why do you use __file__ in
filename = 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 CalculateField

arcpy.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)
0 Kudos
davidmetzler
New Contributor II
Why do you use __file__ in
filename = 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 CalculateField

arcpy.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)


This is returning the path to the script and the fc name. I would like the abspath of the file and the file name.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
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" )
0 Kudos
davidmetzler
New Contributor II
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" )



I have attempted this and when I do the path yielded is C:\Python26\ArcGIS10.0\Lib\idlelib\idle.pyw If I change __file__ to (fc) I get the return of P:\Scripts\scratch\CheckShot.shp' with P:\Scripts\scratch being the path to the script not the shapefile. Any thoughts?


Thanks!

Dave
0 Kudos
JakeSkinner
Esri Esteemed Contributor
If you're looking to obtain the path of the feature class,  try:

filename = "'" + arcpy.env.workspace + os.sep + fc + "'"
0 Kudos
BruceNielsen
Occasional Contributor III
I've always used the CatalogPath property of a FeatureClass or Layer's Describe object.
0 Kudos
davidmetzler
New Contributor II
We have a solution! Here is the code:
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()


Big thanks to JSkinn3
0 Kudos
curtvprice
MVP Esteemed Contributor
I agree with Bruce -- the last one will work even if the workspace is not set.

1. arcpy.env.workspace + os.sep + fc
2. os.path.join(arcpy.env.workspace,fc)
3. arcpy.Describe(fc).catalogPath
0 Kudos