代码点读机 / Python / os.path.exists

os.path.exists Python · 模块功能

检查文件或文件夹是否存在。

怎么用

import os
os.path.exists(path)

小例子

import os
print(os.path.exists('this_file_does_not_exist.txt'))

运行结果

False

提醒:路径可以是相对或绝对。

官方文档:https://docs.python.org/zh-cn/3/library/os.path.html#os.path.exists

在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← Python全部词条