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

Sandra-24 sandravandale at yahoo.com
Sun Dec 17 12:14:31 EST 2006


It looks like you can get a fairly good apporximation for samefile on
win32. Currently I'm using the algorithm suggested by Tim Chase as it
is "good enough" for my needs.

But if one wanted to add samefile to the ntpath module, here's the
algorithm I would suggest:

If the passed files do not exist, apply abspath and normcase to both
and return the result of comparing them for equality as strings.

If both paths pass isfile(), try the mecahnism linked to in this thread
which opens both files at the same time and compares volume and index
information. Return this result. If that raises an error (maybe they
will not allow us to open them) Try comparing them using the approach
suggested by Tim Chase, but if that works there should be some
indication given that the comparison may not be accurate (raise a
warning?). If that also fails, raise an error.

This should allow samfile to be used on win32 in well over 99.9% of
cases with no surprises. For the rest it will either return a result
that is likely correct with a warning of some kind, or it will fail
entirely. It's not perfect, but you can't acheive perfection here. It
would, however, have far less surprises than newbies using == to
compare paths for equality. And it would also make os.path.samefile
available on another platform.

os.path.sameopenfile could be implemented perfectly using the
comparison of volume and index information alone (assuming you can get
a win32 handle out of the open file object, which I think you can)

If someone would be willing to write a patch for the ntpath tests I
would be willing to implement samefile as described above or as agreed
upon in further discussion. Then we can submit it for inclusion in the
stdlib.




More information about the Python-list mailing list