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:30:15 EST 2006
On Sat, 16 Dec 2006 17:02:04 -0800, Sandra-24 wrote:
> 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
Both point to the same file.
> 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.
But for the simple case above:
>>> import os
>>> path = '/home//user/somedirectory/../file'
>>> os.path.normpath(path)
'/home/user/file'
--
Steven.
More information about the Python-list
mailing list