Python递归遍历文件夹批量替换某字符串改名

情景:
在某拼平台买了一门课程,而后发现所有的视频和源代码**都在文件名后面拼接了一段字符串:
“【加微信#######赠送精品IT课程】”
这种问题通过Python写一个小脚本即可
源代码如下:

import os

# rootDir = os.getcwd()
rootDir = os.path.dirname(__file__)

def renameFile(filename):
    # do something
    print('renameing file:', filename)
    oriName = filename
    tarName = filename.replace('{这里写要被替换掉的字符串}', '')

    print('oriname', oriName, '-tar name', tarName)
    os.rename(oriName, tarName)
    
def getAllFiles(path):
    thisSubDir = os.listdir(path)
    for fileOrDir in thisSubDir:
        thisPath = os.path.join(path, fileOrDir)
        if os.path.isdir(thisPath): #如果是文件夹
            getAllFiles(thisPath)   #继续去递归
        else:
            renameFile(thisPath)
            
    
if __name__ == '__main__':
    print('Start processing......')
    print('Start get all files in dir:', rootDir)
    getAllFiles(rootDir)

博客搬家验证:
var code = “c0e95672-984c-4f5e-8ad7-1a32b20a5881”

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值