将目录以及子目录中的广告网址去除

导入模块
import os
定义递归遍历文件夹
def get_dir(start_path):
    names = os.listdir(start_path)
    for name in names:
        in_path = start_path + os.sep + name
        wash_data(in_path)
        if os.path.isdir(in_path):
            get_dir(in_path)
将带有广告的文件全部重命名
def wash_data(path):
    if "【" in path:
        start = path.split("【")[0]
        end = path.split("【")[1].split("】")[1]
        res = start + end
        save_data(res)
        os.rename(path, res)
    else:
        save_data(path)
将改好的文件路径及文件名存入列表中
all_path = []
def save_data(path):
    all_path.append(path)
直接将列表写入储存文件名的txt中,减少IO操作
def write_data(all_path):
    result = "\n".join(all_path)   # os.linesep
    with open("FileName.txt", "a", encoding="utf-8") as f:
        print(result)
        f.write(result)
开始
start_path = r"F:\20-Able-20190430-随机森林"

get_dir(start_path)

write_data(all_path)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hao难懂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值