python批量删除文件

1、查询与删除

mport os
 
def get_files_in_folder(folder_path):
    files = []
    for file in os.listdir(folder_path):
        if os.path.isfile(os.path.join(folder_path, file)):
            files.append(file)
    return files


def del_file(file_to_delete= 'example.txt'):
    # 检查文件是否存在
    if os.path.isfile(file_to_delete):
        # 删除文件
        os.remove(file_to_delete)
        print(f"文件 {file_to_delete} 已删除。")
    else:
        print(f"文件 {file_to_delete} 不存在。")

2、添加模块到地址中

import sys
module_path=r"C:\Users\li\workspace\temp\e\ttt"
sys.path.append(module_path)

3、批量删除多个路径中不需要导出的文件

在excite TD 仿真结果文件夹中,有很多文件不需要导出,在导出前进行批量删除。

>>> caseNames=['0550','0500','0600','0700','0800','0900','1000','1100','1200','1300','1400','1500','1600','1700','1800','1900','2000','2100','2200']
>>> for caseName in caseNames:
	path='E:\\NVH_sim_DDi16\\DDi12D600\\excite-td\\TD-DDi12-D600-1.PSP_OFF.'+caseName+'rpm\\results'
	files=get_files_in_folder(path)
	for file in files:
		if file[0]!='T'or file[1]!='D' or file[2]!='-':
			del_file(path+'/'+file)

在这里插入图片描述

相关推荐

  1. python进行文件批量命名

    2024-04-27 12:38:01       57 阅读
  2. python删除一个文件夹所有文件

    2024-04-27 12:38:01       31 阅读
  3. Python实现批量创建Excel文件

    2024-04-27 12:38:01       60 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-27 12:38:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-27 12:38:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-27 12:38:01       87 阅读
  4. Python语言-面向对象

    2024-04-27 12:38:01       96 阅读

热门阅读

  1. 常见经典目标检测算法

    2024-04-27 12:38:01       30 阅读
  2. openssl3.2 - exp - get openssl version info

    2024-04-27 12:38:01       31 阅读
  3. 比特币中用到的密码学功能【区块链学习笔记1】

    2024-04-27 12:38:01       28 阅读
  4. 力扣经典150题第四十三题:两数之和

    2024-04-27 12:38:01       31 阅读
  5. LlamaFactory源码解析 PPO

    2024-04-27 12:38:01       28 阅读
  6. IOS 纯代码自定义UIView案例

    2024-04-27 12:38:01       30 阅读
  7. 学习 Rust 的第十二天:如何使用向量

    2024-04-27 12:38:01       32 阅读
  8. 4.9 海思SS928开发 - uboot开发 - 环境变量工具使用

    2024-04-27 12:38:01       30 阅读