Select to view content in your preferred language

python expression for changing file path

4580
7
Jump to solution
12-17-2014 08:44 AM
ErikBlake
Regular Contributor

What would be the proper syntax if I want to use the "replace" python function in ArcMap field calculator to replace a file path with a new one.

for example the current links path is W:\folder name\file name I want to replace just the "W:\" with "V:\" Any help would be great. Thanks

0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

Hi Erik,

You could use the following:

!Links!.replace("W:", "V:")

where 'Links' is the field name.

screen1.png

View solution in original post

0 Kudos
7 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Erik,

You could use the following:

!Links!.replace("W:", "V:")

where 'Links' is the field name.

screen1.png

0 Kudos
ErikBlake
Regular Contributor

Thanks Jake.

0 Kudos
ErikBlake
Regular Contributor

Jake

would you possibly know how I would use python to replace "\" with "/" this occurrence happens in the middle of the links string. So for example: V:\folder\folder\filename

I was able to get the "\" replaced up to the last one by using the replace function in arcmap by doing this:

!LINKS!.replace("\foldername" , "/foldername")

However when i try to replace the text ending in the backslash it errors out. For example:

!LINKS!.replace("foldername\" , "foldername/")

Any suggestion would be great help. Thanks.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Maybe you should use some os.path methods:

import os

path = r"V:/blah\blah/blah\blah"
print os.path.normcase(path)
# >> v:\blah\blah\blah\blah

path = r"V://blah\blah/blah\\blah"
print os.path.normpath(path)
# >> V:\blah\blah\blah\blah
0 Kudos
ErikBlake
Regular Contributor

Xander

Thanks for your time and help. Excuse my ignorance, but I have just a little experience in python. So I want to do this on a field in one of our layers that contains these file path to be used to access links through our web maps. Is it still possible to use the method you showed above. And do I do it inside python in arcmap from the geoprocessing toolbar. Thanks again for your help.  

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Try using double back slashes.  Ex:

!Links!.replace("W:\\folder\\folder", "V:\\folder2\\folder2")

0 Kudos
ErikBlake
Regular Contributor

Jake/Xander

Thanks guys for your help. I just ended up exporting the layer to dbf and made the changes in excel and then rejoined back to the layer in ArcMap and did a field calculation. Much easier for me that way. But just wanted to say thanks for your help and suggestions. Trying to get backslashes changed to forward slashes is still a little beyond my skill set with python.

0 Kudos