os.path.exists(path) 和 os.path.lexists(path) 的区别

使用os.path.exists()方法可以直接判断文件是否存在。
代码如下:
>>> import os
>>> os.path.exists(r'C:1.TXT')
False
>>>

os.path.exists(path)
Return True if path refers to an existing path. Returns False for broken symbolic links. On some platforms, this function may return False if permission is not granted to execute os.stat() on the requested file, even if the path physically exists.

os.path.lexists(path)
Return True if path refers to an existing path. Returns True for broken symbolic links. Equivalent to exists() on platforms lackingos.lstat().
主要的区别在于,exists()会自动判断失效的文件链接。如果检查的文件是一个软链接,但这个软连接指向的文件被删除了,会返回False。而lexists()不会做这个检查,只要软连接存在,即使它指向的文件不存在,也返回True。


原文地址:https://www.cnblogs.com/timssd/p/4709629.html