zoukankan      html  css  js  c++  java
  • python批量替换文件名

    替换关键字

    #-*-coding:utf-8-*-
    import os
    import re
    filepath = u'E:\CMMI4\07_测试文档'
    files = os.walk(filepath)
    name = u'这是替换前的关键字'
    rename = u'这是替换后的关键字'
    for folderName, subfolders, filenames in files:
        # print(filenames)
        # print(subfolders)
        # print(folderName)
        for file in filenames:
            # print(file)
            nameRegex = re.compile(r'这是替换前的关键字(.*)')
            partName = nameRegex.search(file).group(1)
            # print(partName)
            newname = rename + partName
            # print(newname)
            oldPath = os.path.join(folderName, file)
            newPath = os.path.join(folderName, newname)
            os.rename(oldPath, newPath)
    print('success!')

    修改文件后缀,把 .docx 变为 .doc ;

    #-*-coding:utf-8-*-
    import os
    import re
    filepath = u'E:\CMMI4\07_测试文档'
    files = os.walk(filepath)
    for folderName, subfolders, filenames in files:
        for file in filenames:
            if file.endswith('.docx'):
                nameRegex = re.compile(r'(.*).docx')
                partName = nameRegex.search(file).group(1)
                newname = partName + '.doc'
                oldPath = os.path.join(folderName, file)
                newPath = os.path.join(folderName, newname)
                os.rename(oldPath, newPath)
    print('success!')
  • 相关阅读:
    IMDB情感分类学习
    torchtext入门学习
    3.1日学习笔记|3.2日学习笔记
    2.24日学习笔记|2.26日学习笔记|2.27|2.28
    快慢指针问题
    2.21日学习笔记|2.22日学习笔记|2.23学习笔记
    dinic模板
    P1247 取火柴游戏 博弈nim
    博弈论
    P2161 [SHOI2009]会场预约 树状数组
  • 原文地址:https://www.cnblogs.com/lansan0701/p/10405702.html
Copyright © 2011-2022 走看看