0

Using Python 2.6 on Windows I have a function that needs to accept a path name as an arg. I'm running into issues when certain specific paths are passed.

C:\users\bob\something.png    #this is handled no prob.
C:\users\bob\nothing.png      #this generates a WindowsError
C:\users\bob\test.png         #this also generates a WindowsError

What I'm gathering is that the \n in the "nothing" path is being interpreted as a new line, and the \t in the "test" path is being interpreted as a tab.

If I print out the path names, that's what appears to be happening.

print os.path.abspath("C:\users\bob\nothing.png")
C:\users\bob
othing.png

Same for the 'test' path, except a tab instead of a new line.

The only thing I've come up with so far is to do a check to see if \n or \t are in the path name, and then handle it accordingly, but there must assuredly be a better way.

if '\n' in path_name:
    #escape slash, move on

What would a better way be?

4

1 回答 1

4
print os.path.abspath(r"C:\users\bob\nothing.png")

可能是你要找的...

虽然用户输入应该自动转义斜线......

>>> a = raw_input("Enter Path:")
Enter Path:C:\www\a\nothing.jpg
>>> a
'C:\\www\\a\\nothing.jpg'

正如您从这个示例中看到的,用户输入确实被转义了

于 2013-05-13T21:38:24.447 回答