Question

Say I have 2 paths:

derp.py
/lol/lel/doge/derp.py

How do I check if both paths refer to the same file? I've seen two ways to do it, but are there any disadvantages to either.

os.path.samefile("derp.py", "/lol/lel/doge/derp.py")
os.path.abspath("derp.py") == os.path.abspath("/lel/lol/doge/derp.py")

I don't particularly care about following symlinks or if one of the files doesn't exist.

Was it helpful?

Solution

If you don't care if any of the files exists, then os.path.samefile() will not work for you, because it actually compares i-node numbers. That leaves you with the only option of comparing the absolute paths.

OTHER TIPS

The former adheres to symlinks, while the latter doesn't. using samefile is the "correct" way if your intention is that these files are the same file. If you just want to check that the paths point at the same place (disregarding symlinks) you can use the latter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top