python中判断文件/目录是否存在

使用os模块

1、判断是否是文件

os.path.isfile('/tmp/yjt.json')

2、判断是否是目录

os.path.isdir('/tmp')

3、判断路径是否存在

os.path.exists(path)

# 使用 access() 方法
os.access(path, os.F_OK)

 

使用pathlib模块

import pathlib

path = pathlib.Path('/tmp/yjt.json')

# 判断路径是否存在
path.exists()

# 判断是否为文件
path.is_file()

# 判断是否为目录
path.is_dir()

 

posted @ 2021-10-10 11:48  八戒不爱吃西瓜  阅读(456)  评论(0编辑  收藏  举报