如何檢查文件是否存在,沒有例外?如何查看文件是否存在,而不使用try口供?
4 回答

蠱毒傳說
TA貢獻(xiàn)1895條經(jīng)驗 獲得超3個贊
isfile()
, exists()
True
isfile()
exists()
>>> print os.path.isfile("/etc/password.txt")
True
>>> print os.path.isfile("/etc")
False
>>> print os.path.isfile("/does/not/exist")
False
>>> print os.path.exists("/etc/password.txt")
True
>>> print os.path.exists("/etc")
True
>>> print os.path.exists("/does/not/exist")
False
添加回答
舉報
0/150
提交
取消