views:

162

answers:

2

I have the following string read from an XML elememnt, and it is assigned to a variable called filename. I don't know how to make this any clearer as saying filename = the following string, without leading someone to think that I have a string literal then.

\\server\data\uploads\0224.1307.Varallo.mov

when I try and pass this to

os.path.basename(filename)

I get the following

\\server\\data\\uploads\x124.1307.Varallo.mov

I tried filename.replace('\\','\\\\') but that doesn't work either. os.path.basename(filename) then returns the following.

\\\\server\\data\\uploads\\0224.1307.Varallo.mov

Notice that the \0 is now not being converted to \x but now it doesn't process the string at all.

what can I do to my filename variable to get this String in a proper state so that os.path.basename() will actually give me back the basename. I am on OSX so the uncpath stuff is not available.

All attempts to replace the \ with \\ manually fail because of the \0 getting converted to \x in the beginning of the basename.

NOTE: this is NOT a string literal so r'' doesn't work.

+2  A: 

We need more information. What exactly is in the variable filename? To answer, use print repr(filename) and add the results to your question above.


Wild guess

DISCLAIMER: This is a guess - try:

import ntpath
print ntpath.basename(filename)
nosklo
This is the syntax for a windows file share, so the windows path module still should be used. Using ntpath works just fine on Mac and does what you want: typing `ntpath.basename(r'\\server\data\uploads\0224.1307.Varallo.mov')` gives `0224.1307.Varallo.mov` as expected.
Clueless
__READ FOR COMPREHENSION__ I AM NOT WORKING WITH A STRING LITERAL!
fuzzy lollipop
@fuzzy lollipop, the ntpath module exists on OSX and is exactly what you need to deal with UNC paths. Did you try this?
gnibbler
+1  A: 

All the downvoting in the world won't change the fact that you're doing it wrong. os.path is for native paths. \\foo\bar\baz is not a OS X path, it's a Windows UNC. posixpath is not equipped to handle UNCs; ntpath is.

Ignacio Vazquez-Abrams