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!
Solved! Go to Solution.
>>> 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
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
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!
>>> 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
Thanks Dan. I spent an age stuffing around with .replace, your solution is the one that I didn't try!
Problem solved.
Now that you have had some coffee... /blogs/dan_patterson/2016/08/14/filenames-and-file-paths-in-python