Python's os.path.samefile
function tells whether two paths point to the same file.
If a
is the path to a symlink that points to b
, will os.path.samefile("a", "b")
return True
or False
? Is that guaranteed across OSes and filesystems?
Python's os.path.samefile
function tells whether two paths point to the same file.
If a
is the path to a symlink that points to b
, will os.path.samefile("a", "b")
return True
or False
? Is that guaranteed across OSes and filesystems?
As you've already found, os.path.samefile
uses os.stat
. stat follows symbolic links:
This function normally follows symlinks; to stat a symlink add the argument follow_symlinks=False, or use lstat().
This function can support specifying a file descriptor and not following symlinks.
Back to your question:
If
a
is the path to a symlink that points tob
, willos.path.samefile("a", "b")
returnTrue
orFalse
?
This should return True
since stat follows symbolic links by default.
If you need a function that returns False
in this case, you can simply create your own using the code you've already found, and the follow_symlinks=False
with stat.
Is that guaranteed across OSes and filesystems?
That's a tough one. Even though many high-level languages try to to hide the details of the underlying system from the user, some operations are almost always system dependent. File IO often falls in this category. In fact, File IO is also file system dependent – if you have two different file systems mounted on the same host, the same code might behave differently on both file systems.
In practice, most file systems behave similarly in most cases – you just need to be aware that edge cases do exist. This is why standards exist. If your system is POSIX-compliant, SUS-compliant, or UNIX-based. It's likely True
will always be returned.