如何检查python中是否存在文件?

Zyg*_*tas 3 python windows file

可能重复:
检查文件是否存在的Pythonic方法?

如何检查python中是否存在文件?

在Windows中编写脚本

bra*_*ers 11

使用该os模块非常容易.

from os.path import exists
print exists("C:\somefile.txt")
Run Code Online (Sandbox Code Playgroud)


Gar*_*Jax 6

查看手册,这是一个非常简单的问题:

http://docs.python.org/library/os.path.html#os.path.exists

os.path.exists(path)
Run Code Online (Sandbox Code Playgroud)

如果path指向现有路径,则返回True.对于损坏的符号链接,返回False.在某些平台上,如果未授予对所请求文件执行os.stat()的权限,则此函数可能返回False,即使路径实际存在也是如此.