Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

share|improve this question
2  
As a side note, there is an arcpy.exists() command that may play more nicely with odd file and folder names than os.path.exists. –  dmahr Jun 18 '13 at 18:19
1  
Have you tried using arcpy.GetParameterAsText(1) instead of sys.argv[2]? –  nmpeterson Jun 18 '13 at 18:36
    
I am not seeing the problem you describe at 10.0. Can you post screenshots of your tool parameters and what you saw that led you to believe that the path is not being interpreted correctly? –  blah238 Jun 19 '13 at 21:26
add comment

3 Answers

As you have discovered, Windows paths contain a single backslash, but in Python a single backslash is an escape character.

You have a few options:

1) You can use a raw string (r"stringgoeshere"), or os.path.normpath(), as detailed in this blog post.

2) In order for Python to understand that a string contains a path you need to use double backslashes.

So your path should be formatted as such:

(C:\\Data\\Hardware\\Folder)

A simple alternative way to accomplish this in your code would be to assign sys.argv[2] to a string and format it from there.

share|improve this answer
    
-1 because this advice only applies to Python string literals. Strings interpreted from the command line arguments (sys.argv or arpcy.GetParameter(AsText)) are already string objects and since a backslash is not an escape character in the Windows shell, you should not escape backslashes anywhere except in Python source code (i.e. string literals). –  blah238 Jun 19 '13 at 21:26
add comment

Something does not add up with either the question or existing answers. The backslash is only an escape character for string literals in Python source code, not on the Windows shell (which is what Python uses on Windows to parse command line arguments).

Running this simple script:

import os, sys, arcpy

arcpy.AddMessage(str(os.path.exists(sys.argv[1])))

From an ArcGIS 10.0 script tool with a single input parameter of type Folder, results in "True" being printed for any existing folder.

share|improve this answer
    
I guess my biggest mistake may have been that once dialog box parameter passed its value to sys.argv[2], I then passed the content of sys.argv[2] into a variable called output_location for readability purpose. In passing path from sys.argv[2] to variable output_location the slicing may have taken place. Unfortunately I am not able to test this theory since I am not currently at my machine. Thank you all for the feedback. –  lemuel Jun 20 '13 at 0:34
    
That would not explain it either. –  blah238 Jun 20 '13 at 1:33
add comment

I feel like you may just be over-complicating things here. Why are you fighting to pass the input parameter using the %s string formatting? Two working code examples:

import sys,os

#set the input parameter as a variable
var = sys.argv[1]

if os.path.exists(var):
    print "exists"

else:
    print "fails"
    #Do Something Magical

Or simply:

import sys,os

#Just pass the input parameter directly to the exists function
if os.path.exists(sys.argv[1]):
    print "exists"

else:
    print "fails"
    #Do Something Magical

And @blah238 is right. If you are doing this as an ArcMap Toolbox Tool, you should really be using arpcy.GetParameter(AsText).

share|improve this answer
    
Thank you for the examples, really helped out a lot! –  lemuel Jul 22 '13 at 18:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.