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

查看:24
本文介绍了检查文件是否存在的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)

如果 path 是现有的,则返回 True常规文件.这遵循象征性链接,所以 islink()isfile() 可以为真路径.

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天全站免登陆