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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6908003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:39:40  来源:igfitidea点击:

How to check if a file exists in python?

pythonwindowsfile

提问by Zygimantas

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

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

How to check if a file exists in python?

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

Scripting in Windows

在 Windows 中编写脚本

回答by bradley.ayers

This is very easy using the osmodule.

使用该os模块非常容易。

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

回答by GaretJax

Check the manual, this is a really easy question:

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

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

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.

os.path.exists(path)

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

回答by Amber

http://docs.python.org/library/os.path.html#os.path.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

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