File Path Lists and arcpy.GetParameterAsText

2474
5
Jump to solution
09-18-2016 08:48 PM
AnthonyCheesman1
Occasional Contributor II

This probably comes under the category of  'let me Google that for you', but anyway:

I'm trying to write an ArcToolbox tool using a Python script I've already created. The script relies on a list of files, of which I've set up in the toolbox parameters as type 'Any' and multivalue = Yes.

My issue is that if I use arcpy.GetParameterAsText, the variable returns a string, which can be converted to a list by .split(';'), but then the backslashes used in the file path do strange things to my output.

Try as I may, I cannot think of an easy way of converting '\' to '\\' to solve my issue.

I've tried using arcpy.GetParameter, but this does not suit my need, as this returns a list of geoprocessing objects, and I need to extract part of the file name for a subsequent step in my process. Unless there's an easy way to convert this GP object to a text name?

Feeling a little fragile after the weekend, and my brain is not comprehending this particular issue on a Monday! Please help!

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Esteemed Contributor
>>> p = "c:\path1;c:\path2"
>>> pths = p.replace("\\","/").split(";")
>>> pths
['c:/path1', 'c:/path2']

if you can't append an 'r' to the paths, don't worry about it, just replace and split.  forward slashes are just fine if you can't do the double backslashes.

There are other ways, but it is Monday

View solution in original post

5 Replies
ModyBuchbinder
Esri Regular Contributor

Try using "r" in front of the string. It just like the "@" in C#

It tell the software to ignore the usual meaning of the slash and use it as simple char.

x = r"C:\temp"

Have fun

AnthonyCheesman1
Occasional Contributor II

Thanks Mody. The issue was (is?) was converting the string I already had to the 'raw' string.

Anyway - after lunch and a coffee, I was able to troubleshoot the issue and get a resolution. I've gone back to using arcpy.GetParameter, and then using arcpy.Describe on the geoprocessing object to extract the file name and path, and then using os.path.join to concatenate the two.

Convoluted but it seems to be working!

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor
>>> p = "c:\path1;c:\path2"
>>> pths = p.replace("\\","/").split(";")
>>> pths
['c:/path1', 'c:/path2']

if you can't append an 'r' to the paths, don't worry about it, just replace and split.  forward slashes are just fine if you can't do the double backslashes.

There are other ways, but it is Monday

AnthonyCheesman1
Occasional Contributor II

Thanks Dan. I spent an age stuffing around with .replace, your solution is the one that I didn't try!

Problem solved.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor
0 Kudos