zoukankan      html  css  js  c++  java
  • python--批量修改文件夹名

    python代码如下:

    import os , re
    import os.path
    
    rootdir = r'C:UsersAdministratorDesktop222'
    # rootdir = os.path.abspath(rootdir)
    
    print('absolute root path:
    *** ' + rootdir + ' ***')
    # 先修改文件名,循环文件夹和子文件夹,文件名
    for dirname, subdir, filenames in os.walk(rootdir):
       for filename in filenames:
          pathfile = os.path.join(dirname, filename)
          euroFilename = re.search(r'([a-zA-Z]+)(-|$)?([a-zA-Z]+)',filename)
          mvfile = euroFilename.group() + '.yml'
          mvpathfile = os.path.join(dirname,mvfile)
          if 'shtel' in filename:
             continue
          elif 'gateway' in filename:
             continue
          os.rename(pathfile, mvpathfile)
    #在改文件夹名
    for parent, dirnames, filenames in os.walk(rootdir, topdown=False):
       for dirname in dirnames:
          pathdir = os.path.join(parent, dirname)
          euroFilename = re.search(r"(stage)?-?([a-zA-Z1]+)(-[a-zA-Z1]+)?(-conf)?(-master)?", dirname)
          mvpathdir = os.path.join(parent, euroFilename.group())
          os.rename(pathdir, mvpathdir)
          pathname = [euroFilename.group(2), euroFilename.group(3), euroFilename.group(5)]
          if pathname[1] == None:
             continue
          pathdirs = ''.join(pathname)
          pathnames = os.path.join(parent, pathdirs)
          os.rename(mvpathdir, pathnames)     #去掉文件夹的stage字段
    
    
  • 相关阅读:
    day07_final
    day06_final
    day02_final
    day04_final
    New
    AtCoder Grand Contest 015 E Mr.Aoki Incubator
    长链剖分学习笔记
    关于某些莫队的优化
    CodePlus 2019 3月月赛 Div.1 A题 TREE
    边分治学习笔记
  • 原文地址:https://www.cnblogs.com/fan-gx/p/10823256.html
Copyright © 2011-2022 走看看