Monday, 22 August 2016

arcpy - Python - formatting path strings and checking if a path exists



I would like to use the following code to determine if a path exists. The path I would like to check is being passed to my code via parameter on a ArcMap 10.0 tool dialog box. The parameters data type is folder, this folder path is being passed along to sys.argv[2] in my code below.


{import sys

import os

if os.path.exists("%s"): %(sys.argv[2])

pass

else:


#Do Something Magical}

Normally I would not have any issue with this script if I were explicitly stating the folder path. For example any of the following three works well and does what I need:


os.path.exists("C:\\Data\\Hardwar\\Folder"):


os.path.exists(r"C:\Data\Hardwar\Folder"):


os.path.exists("C:/Data/Hardwar/Folder"):


In the preceding code what I am finding is that as the path i.e. (C:\Data\Hardwar\Folder) is being passed from my parameter to sys.argv[2] the first letter after \ is being escaped and rightfully so.
How do I format the path in sys.argv[2] to a python acceptable path format?




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...