Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
假设我有 2 条路径:
derp.py /lol/lel/doge/derp.py
如何检查两个路径是否引用同一个文件?我已经看到了两种方法来做到这一点,但是任何一种都有缺点。
os.path.samefile("derp.py", "/lol/lel/doge/derp.py") os.path.abspath("derp.py") == os.path.abspath("/lel/lol/doge/derp.py")
我并不特别关心以下符号链接或其中一个文件是否不存在。
如果您不关心是否存在任何文件,那么os.path.samefile()它对您不起作用,因为它实际上比较了 i-node 编号。这使您只能选择比较绝对路径。
os.path.samefile()
前者坚持符号链接,而后者不坚持。samefile如果您的意图是这些文件是同一个文件,则using是“正确”的方式。如果您只想检查路径是否指向同一位置(忽略符号链接),您可以使用后者。
samefile