关于Windows:如何检查python中是否存在文件?

How to check if a file exists in python?

本问题已经有最佳答案,请猛点这里访问。

Possible Duplicate:
Pythonic way to check if a file exists?

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

在Windows中编写脚本


这是非常容易使用os模块。

1
2
from os.path import exists
print exists("C:\somefile.txt")


检查手册,这是一个很简单的问题:

http://docs.python.org /图书馆/ os.path.html # os.path.exists

1
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.


http://docs.python.org /图书馆/ os.path.html # os.path.exists

所以,如果你想确保说文件(AS路径实际上是反对一个目录,链接等):

http://docs.python.org /图书馆/ os.path.html # os.path.isfile