开发者

How to check if a file exists in python? [duplicate]

This question already has answers here: 开发者_开发知识库 Closed 11 years ago.

Possible Duplicate:

Pythonic way to check if a file exists?

How to check if a file exists in python?

Scripting in Windows


This is very easy using the os module.

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


Check the manual, this is a really easy question:

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

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/library/os.path.html#os.path.exists

or if you want to also ensure that said path is actually a file (as opposed to a directory, link, etc):

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜