How to test if two strings point to the same file or directory?
Tim Chase
python.list at tim.thechases.com
Sat Dec 16 20:59:43 EST 2006
>>> Comparing file system paths as strings is very brittle. Is there a
>>> better way to test if two paths point to the same file or directory
>>> (and that will work across platforms?)
>> os.path.samefile(filename1, filename2)
>> os.path.sameopenfile(fileobject1, fileobject2)
>
> Nice try, but they don't "work across platforms".
Okay...double-checking the docs.python.org writeup, it apparently
does "work across platforms" (Mac & Unix), just not "across *all*
platforms", with Win32 being the obvious outlier. It seems a
strange omission from Win32 python, even if it were filled in
with only a stub...something like
def samefile(f1, f2):
return abspath(f1.lower()) == abspath(f2.lower())
it might not so gracefully handle UNC-named files, or SUBST'ed
file-paths, but at least it provides an attempt at providing the
functionality on win32. As it currently stands, it would be
entirely too easy for a [Mac|*nix] programmer to see that there's
a samefile() function available, use it successfully based on its
docstring, only to have it throw an exception or silently fail
when run on win32.
-tkc
More information about the Python-list
mailing list