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.

Était-ce utile?

La 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.

Autres conseils

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top