Select to view content in your preferred language

Python for renaming file paths

2074
9
04-28-2017 06:33 AM
KelseyQuinlan
Deactivated User

Hello all,

I am fairly new to python and am having issues renaming file paths within a table. I need to replace all of my \ for / within a field. \ is an escape so it is not working. Here is a brief snippet of my code:

arcpy.CalculateField_management(in_table="Assess", field="IMAGELINK", expression="""!IMAGELINK!.replace(r"\","/")""", expression_type="PYTHON_9.3", code_block="")

I know there are easy ways to do this with VB and with field calculator but I am trying to automatic a much bigger table update.

edit: I've tried using \ \ instead and still had no luck.

edit 2: This is ultimately what worked for me, thank you Joshua

expression=r"""!IMAGELINK!.replace("\\","/")"""

Any advice is much appreciated! Thanks

0 Kudos
9 Replies
JoshuaBixby
MVP Esteemed Contributor

What about escaping the escape character "\\" .

0 Kudos
KelseyQuinlan
Deactivated User

Yes, unfortunately " \ \" still bombs the script. not quite sure what I am missing here, I thought that would be the easy solution too.

Thanks for the reply!

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

What do you mean by "bomb?"  Providing the specific error message is helpful for those trying to help resolve your issue.

0 Kudos
KelseyQuinlan
Deactivated User

Sure thing,

New Script: arcpy.CalculateField_management(in_table="AssessVP", field="IMAGELINK", expression="""!IMAGELINK!.replace(r"\\","/")""", expression_type="PYTHON_9.3", code_block="")

Error Message: Runtime error Traceback (most recent call last): File "<string>", line 1, in <module> File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\management.py", line 3457, in CalculateField raise e ExecuteError: ERROR 000539: SyntaxError: EOL while scanning string literal (<expression>, line 1) Failed to execute (CalculateField).

0 Kudos
DanPatterson_Retired
MVP Emeritus

you are tripping over double quotes...

(r"\\","/") blah blah

try (r'\\', '/') blah blah

a couple of single quotes might go a long way

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

It isn't uncommon to run into issues with backslashes in strings within strings when passing arguments to geoprocessing tools.  I believe either of the following will work:

expression=r"""!IMAGELINK!.replace("\\","/")"""
# or
expression="""!IMAGELINK!.replace("\\\\","/")"""‍‍‍‍‍‍
0 Kudos
KelseyQuinlan
Deactivated User

Thank you! That did the trick

I really appreciate everybody's input!

0 Kudos
curtvprice
MVP Esteemed Contributor

Here's how I would do it.

This avoids having to handle the complex string literals by using os.normpath (which converts all delimiters [including duplicated ones] to \ on Windows), os.sep (which allows you to get at \ without having to specify it with a string literal). Lastly, using an arcpy.da  UpdateCursor instead of Calculate Field avoids the complexity of a code block (and, very fastly [you said this was a big table]).

import os
with arcpy.da.UpdateCursor("Assess", "IMAGELINK") as rows:
    for row in rows:
        row[0] = os.path.normpath(row[0]).replace(os.sep, "/")
        rows.updateRow(row)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
JeffJacobson
Frequent Contributor

Have you tried using arcpy.da.UpdateCursor rather than the Calculate Field tool? That's how I would accomplish this task.

0 Kudos