用Pythonic方式检查文件是否存在? [英] Pythonic way to check if a file exists?

查看:73
本文介绍了用Pythonic方式检查文件是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪个是检查文件是否存在,而不是创建它的首选方式?

Which is the preferred way to check if a file exists and if not create it?

推荐答案

要检查一个路径是现有文件:

To check if a path is an existing file:

os.path.isfile(path)


返回 True 如果路径是现有的
常规文件。这符号是
链接,所以 islink()
isfile()可以对于相同的
路径,为true。

Return True if path is an existing regular file. This follows symbolic links, so both islink() and isfile() can be true for the same path.

这篇关于用Pythonic方式检查文件是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆