This question is different to Using Calculate Field to fix hyperlinks in ArcPy gives Cannot use VB for services? which was asked previously because it is using the Python parser specifically, rather than VB.

Syntax calculates the field correctly within tool (see raw below). Copy the python snippet directly from the results window to migrate into main script, and the syntax returns as invalid. Not sure how to fix.

Raw (working):

"\\\\ftp\\raw\\" + "20160206" + "\\Win\\Charles\\" + !NAME! + ".shp"

Copied snippet verbatim (Invalid):

arcpy.CalculateField_management(outShp, "Link", """"////ftp//raw//" + "20160206" + "//Win//Charles//" + !NAME! + ".shp"""", "PYTHON_9.3","#")

Having trouble getting it to run properly, have tried adding 'r' and using different combinations of escapes and it just won't run.

  • 1
    Didn't think this one would be that hard, but after an hour of trying the closest I got was \\\\ftp\\raw\\20160206\\Win\\Charles\\ u"value" .shp which was almost but not quite. (where value was the value in my !name! field)
    – Midavalo
    Commented Feb 9, 2016 at 7:03

1 Answer 1


Ah! Firstly consult rules for python string literals when there is a mix of both single and double quotes.

Here expression is a string that may need to be mixed of single and double quotes. You can not enclose double quotes with double quotes or single quotes with single quotes without special sanitization.

There are couple of ways you can assign your expression in the tool when you need to mix both single and double quotes -

  1. Enclose string that have only double quote into single quotes e.g. '"////ftp//raw//" + "20160206" + "//Win//Charles//"+ str(!NAME!) + ".shp"'

  2. Enclose string that have only single quote into double quotes e.g. "'////ftp//raw//' + '20160206' + '//Win//Charles//'+ str(!NAME!) + '.shp'"

  3. Escape (using \) the quotes (either single or double) e.g. "\"////ftp//raw//\" + \"20160206\" + \"//Win//Charles//\"+ str(!NAME!) + \".shp\"" OR "\'////ftp//raw//\' + \'20160206\' + \'//Win//Charles//\'+ str(!NAME!) + \'.shp\'" or '\"////ftp//raw//\" + \"20160206\" + \"//Win//Charles//\"+ str(!NAME!) + \".shp\"'

  4. Use seperator that gives single expression e.g. parenthesis e.g. """("////ftp//raw//" + "20160206" + "//Win//Charles//"+ str(!NAME!) + ".shp")""" or '''("////ftp//raw//" + "20160206" + "//Win//Charles//"+ str(!NAME!) + ".shp")'''

  • This works perfectly, just out of curiosity, if I wanted to have a dynamic parameter (such as date), how would that insert into the expression?. e.g instead of a hard coded date "20160206" Parameter would be set as date = "20160101"
    – geodranic
    Commented Feb 9, 2016 at 7:52
  • 1
    @StevenFerronato Yeah! Date is datetime object, you can easily convert to string e.g. datetime.strftime('%m/%d/%Y') details docs.python.org/2/library/datetime.html
    – Learner
    Commented Feb 9, 2016 at 7:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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