os.path.isdir('\\\\server\\share') returns negative result when
os.path.listdir('\\\\server\\share') produces a dir listing.
Checking groups.google and the Python faq I found
8.6 Why does o.path.isdir() failon NT shared directories
Which implies it should work if a tralling '\\' is added
but
os.path.isdir('\\\\server\\share\\') still returns negative result
However os.path.isdir('\\\\server\\share\\\\') returns true.
Is this sensible/to be trusted.
As an aside;
using os.path.isdir(r'\\server\share\') results in a complaint of
invalid token - I thought the r prefix meant use raw string, so why the
error ?
--
___
|im ---- ARM Powered ----
the trailing \ is escaping the ending ' of the r'...' string. I just ran
across the caution about having odd numbers of \ at the end of an r'...'
string last evening.
HTH,
Dave LeBlanc
Seattle, WA USA
There is a bug for this already:
http://python.org/sf/513572
I tend to use this function:
def _isdir(dirname):
"""os.path.isdir() doesn't work for UNC mount points. Fake it.
# For an existing mount point (want: _isdir() == 1)
os.path.ismount(r"\\crimper\apps") -> 1
os.path.exists(r"\\crimper\apps") -> 0
os.path.isdir(r"\\crimper\apps") -> 0
os.listdir(r"\\crimper\apps") -> [...contents...]
# For a non-existant mount point (want: _isdir() == 0)
os.path.ismount(r"\\crimper\foo") -> 1
os.path.exists(r"\\crimper\foo") -> 0
os.path.isdir(r"\\crimper\foo") -> 0
os.listdir(r"\\crimper\foo") -> WindowsError
# For an existing dir under a mount point (want: _isdir() == 1)
os.path.mount(r"\\crimper\apps\Komodo") -> 0
os.path.exists(r"\\crimper\apps\Komodo") -> 1
os.path.isdir(r"\\crimper\apps\Komodo") -> 1
os.listdir(r"\\crimper\apps\Komodo") -> [...contents...]
# For a non-existant dir/file under a mount point (want: _isdir() ==
# 0)
os.path.ismount(r"\\crimper\apps\foo") -> 0
os.path.exists(r"\\crimper\apps\foo") -> 0
os.path.isdir(r"\\crimper\apps\foo") -> 0
os.listdir(r"\\crimper\apps\foo") -> [] # as if empty contents
# For an existing file under a mount point (want: _isdir() == 0)
os.path.ismount(r"\\crimper\apps\Komodo\latest.komodo-devel.txt") -> 0
os.path.exists(r"\\crimper\apps\Komodo\latest.komodo-devel.txt") -> 1
os.path.isdir(r"\\crimper\apps\Komodo\latest.komodo-devel.txt") -> 0
os.listdir(r"\\crimper\apps\Komodo\latest.komodo-devel.txt") -> WindowsError
"""
if sys.platform[:3] == 'win' and dirname[:2] == r'\\':
if os.path.exists(dirname):
return os.path.isdir(dirname)
try:
os.listdir(dirname)
except WindowsError:
return 0
else:
return os.path.ismount(dirname)
else:
return os.path.isdir(dirname)
--
Trent Mick
Tre...@ActiveState.com
I noticed this a while back. It doesn't seem to make sense. I thought
nothing inside a raw was escaped, so why is the last character? I suppose
you can resort to stuff like this:
path = os.sep.join(('','','server','share',''))
or this:
path = r'\\server\share' + '\\'
or just give up and do the old:
path = '\\\\server\\share\\'
but I thought the idea behind the raw strings was to save a little typing
and make code a bit more readable since, as in c/c++ you can do everything
with cooked strings, it is just more tedious and ugly.
I see in the Python doc where it says
"""
r"\" is not a value [sic. -- I think he intended "valid"] string literal
(even a raw string cannot end in an odd number of backslashes).
Specifically, a raw string cannot end in a single backslash (since the
backslash would escape the following quote character).
"""
I don't understand why this is the case, though. Is a raw string really
treated as a standard string until some later time when it is translated to
raw?