在 Python 中,可以使用os.path
模块中的os.path.exists(path)
函数来判断某文件是否存在。示例代码如下:
importosif os.path.exists('path/to/file'):print('The file exists.')else:print('The file does not exist.')
如果文件存在,该函数会返回True
,否则会返回False
。 也可以使用os.path.isfile(path)
来判断是不是文件
如果觉得《python如何判断某文件是否存在》对你有帮助,请点赞、收藏,并留下你的观点哦!