문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top