import os
import re
def check_flie(filePath):
# 统计删除总数
count = 0
# 删除已经生成的data_*.js 答卷js文件
for file_path, empty_list, file_name_list in os.walk(filePath):
# file_name_list该列表是存放目标目录中所有文件名
for file_name in file_name_list:
# 正则匹配需要删除的文件--根据需求修改正则表达式
if re.match(r'^data_*', file_name):
# 删除匹配到的文件
os.remove(file_path + file_name)
# 每删除一个文件+1
count += 1
if __name__ == '__main__':
# 目标文件路径--根据需求变更
filePath = '/root/work_code/home/'
check_flie(filePath)
Python中批量删除匹配到的文件
最新推荐文章于 2023-07-04 19:03:39 发布