两种检查方式
- 一种是调用os.path模块中的方法isfile()
- 另一种是使用pathlib模块,在Python2中需要以第三方模块的方法安装,在Python3中pathlib是内置模块,无需安装
- 其实还可以直接使用open函数打开该文件,通过抛出的异常类型来判断文件是否存在,非常不建议使用这种方法。
实例展示
import os.path
import pathlib
# 检查给定路径是否是文件,能够区分文件和目录
print(os.path.isfile('/etc/passwd')) # True
print(os.path.isfile('/etc')) # False
print(os.path.isfile('/does/not/exists')) # False
# 检查给定路径是否存在,无法能够区分文件和目录
print(os.path.exists('/etc/passwd')) # True
print(os.path.exists('/etc')) # True
print(os.path.exists('/does/not/exists')) # False
# Python2需要安装pathlib, Python3中pathlib是内置模块,无需安装
path = pathlib.Path("/etc/passwd")
print(path.exists()) # True
print(path.is_file()) # True
print(path.is_dir()) # False
代码下载
本系列文章和代码已经作为项目归档到github,仓库地址:jumper2014/PyCodeComplete。大家觉得有帮助就请在github上star一下,你的支持是我更新的动力。什么?你没有github账号?学习Python怎么可以没有github账号呢,快去注册一个啦!