美文网首页
Python判断文件是否存在

Python判断文件是否存在

作者: 逍遥_yjz | 来源:发表于2021-07-29 15:17 被阅读0次

1. 判断目录是否存在

now_str = datetime.datetime.now().strftime('%Y-%m%d')
year_name = now_str.split('-')[0]
month_name = now_str.split('-')[1]
# 判断目录是否存在
year_path = os.path.join(path, year_name)
if not os.path.isdir(year_path):
    os.mkdir(year_path)
month_path = os.path.join(path, year_name+'/'+month_name)
if not os.path.isdir(month_path):
    os.mkdir(month_path)

2. 判断文件是否存在

if os.path.isfile(pic_path):
    print('存在')

相关文章

网友评论

      本文标题:Python判断文件是否存在

      本文链接:https://www.haomeiwen.com/subject/pjzxvltx.html