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:43:35 EST 2006
>> Comparing file system paths as strings is very brittle.
>
> Why do you say that? Are you thinking of something like this?
>
> /home//user/somedirectory/../file
> /home/user/file
Or even
~/file
> How complicated do you want to get? If you are thinking about aliases,
> hard links, shortcuts, SMB shares and other complications, I'd be
> surprised if there is a simple way.
>
> But for the simple case above:
>
>>>> import os
>>>> path = '/home//user/somedirectory/../file'
>>>> os.path.normpath(path)
> '/home/user/file'
I'd suggest os.path.samefile which should handle case-sensitive
(non-win32) vs case-insensitive (win32) filenames, soft-links,
and hard-links. Not sure it's prescient enough to know if you
have two remote shares, it will unwind them to their full
server-path name. Works here on my various boxes (Linux, MacOS-X
and OpenBSD) here. I'd assume it's the same functionality on Win32.
-tkc
More information about the Python-list
mailing list