栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python中判断文件夹是否存在,以及新建文件夹

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python中判断文件夹是否存在,以及新建文件夹

import os
from pathlib import Path
import shutil


def cache_file():
    cache_file = Path("cache_file")
    if cache_file.is_dir() == False:
        os.makedirs(cache_file)
        print('缓存目录不存在新建成功')
    else:
        shutil.rmtree(cache_file)
        print('缓存目录存在,删除缓存目录')
        os.makedirs(cache_file)
        print('删除后_新建缓存目录')

cache_file()

def Video_cache_file(Video_id):
    '''判断视频id文件是否存在,如果不存在则新建,并且新建子文件夹image
        如果文件夹存在,则删除文件夹,并且重新新建视频id文件夹,并且新建image子文件夹'''
    Video_id_file = Path("cache_file/"+Video_id)
    if Video_id_file.is_dir() == False:
        os.makedirs(Video_id_file)
        Video_id_file = Path("cache_file/" + Video_id + '/image')
        os.makedirs(Video_id_file)
    else:
        shutil.rmtree(Video_id_file)
        os.makedirs(Video_id_file)
        Video_id_file = Path("cache_file/" + Video_id + '/image')
        os.makedirs(Video_id_file)


Video_cache_file('976729037862800348')

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/461631.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号