How to test if two strings point to the same file or directory?

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Sat Dec 16 20:46:02 EST 2006


On Sun, 17 Dec 2006 12:30:15 +1100, Steven D'Aprano wrote:

>> Is there a
>> better way to test if two paths point to the same file or directory
>> (and that will work across platforms?)
> 
> 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.

Almost, but not quite, platform independent.

os.path.samefile(path1, path2)

>From the docs:

samefile(path1, path2)
    Return True if both pathname arguments refer to the same file or
    directory (as indicated by device number and i-node number). Raise an
    exception if a os.stat() call on either pathname fails. Availability:
    Macintosh, Unix

http://docs.python.org/lib/module-os.path.html


-- 
Steven.




More information about the Python-list mailing list