首页 > 编程语言 >python删除目录及其子目录的所有文件

python删除目录及其子目录的所有文件

时间:2023-01-12 20:33:53浏览次数:43  
标签:__ 删除 filepath python 子目录 del file path os

python3.9的环境:

import os
import sys
import shutil
from pathlib import Path

def del_file(filepath):
del_list = os.listdir(filepath)
for f in del_list:
file_path = os.path.join(filepath, f)
if os.path.isfile(file_path):
os.remove(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
os.rmdir(filepath)


#删除指定目录下的所有文件及其子目录
if __name__ == '__main__':
dir = Path(r'D:\datasets\gen_bigmoney_str\train')
#src = sys.argv[1]
del_file(dir)

标签:__,删除,filepath,python,子目录,del,file,path,os
From: https://blog.51cto.com/u_8681773/6004640

相关文章

  • python | base64隐写
    python|base64隐写闲得无聊写了一个base64隐写库。叫b64steg安装直接pipinstallb64steg即可。项目地址:https://github.com/Mz1z/b64steg......
  • python opencv遍历图像数据集是否存在错误
    python3.9的环境,opencv3.4:平时在准备图像数据集是,有可能其中有个别图像错误引起在深度学习训练到一半时报错,所有先检查一下数据集中的图像是否有错误图像:importosimportc......
  • python opencv通过读取图像数据列表文件来检查图像数据是否存在错误
    python3.9环境,opencv3.4:平时在准备深度学习数据集时,会有图像和对应的图像列表文件,可以使用opencv通过列表文件来读取图像,看是否存在错误数据:使用python脚本来检查数据:impor......
  • 栈和寄存器虚拟机比较(以python和lua为例)
    指令长度pythonpython的指令定长,长度为16bit,其中8bit操作码,8bit操作数。///@file:Python-3.6.0\Include\code.htypedefuint16_t_Py_CODEUNIT;#ifdefWORDS_BIGEND......
  • [oeasy]python0045_四种进制_binary_octal_decimal_hexadecimal
    四种进制回忆上次内容上次研究了通过八进制数值转义\ooo把(ooo)8进制对应的ascii字符输出转义序列\n、\t是转义序列\xhh也是转义序列\ooo还......
  • [oeasy]python0045_四种进制_binary_octal_decimal_hexadecimal
    四种进制回忆上次内容上次研究了通过八进制数值转义​​\ooo​​把(​​ooo​​)<sub>​​8进制​​</sub>对应的​​ascii​​字符输出转义序列​​\n​​、​​\t​​......
  • Python----类和面向对象
    #驼峰命名法classCall_Phone():#类当中的函数叫类方法,类下面添加函数时会自动添加self.self代表类的实例#类的方法必须有一个额外的参数,惯例为self,后面......
  • python udp 接收图片并保存在本地
     疑问1.发送图片是以什么格式2.字节数据怎么保存到本地3.怎么对传输不同设备发送的图片进行分类存储4.udp实现解答1.以字节a.先用cv......
  • python 队列
    将(a,b)这样格式的数据放在队列,,get取得时候报错img=cv.imread('../xavier/UDP/qqq.jpg')img_encode=cv.imencode('.jpg',img)[1]data_encode=np.array(img_encode)dat......
  • python生成应用程序的块截方式
    代码:importos,winshellfromwin32com.clientimportDispatchpath=r"D:/workspace/wwwroot82/pyjiankong/dist/test.lnk"#Pathtobesaved(shortcut)target......