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])



    #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:




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?

  • 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

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:


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.

  • -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

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


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

  • 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

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"

    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"

    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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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