代码之家  ›  专栏  ›  技术社区  ›  ITJ

Python:检索和重命名目录中的索引文件

  •  0
  • ITJ  · 技术社区  · 7 年前

    e、 g如果目录包含以下文件>&燃气轮机;(bar001.txt、bar004.txt、bar007.txt、foo2.txt、foo5.txt、morty.dat、rick.py)。我的脚本应该能够“仅”重命名索引文件,并像这样缩小差距>&燃气轮机;(bar001.txt、bar002.txt、bar003.txt、foo1.txt、foo2.txt…)。

    我把完整的脚本放在下面,它不起作用。该错误是合乎逻辑的,因为没有给出错误消息,但目录中的文件保持不变。

    #! python3
    
    import os, re
    
    working_dir = os.path.abspath('.')
    
    # A regex pattern that matches files with prefix,numbering and then extension
    pattern = re.compile(r'''
        ^(.*?)        # text before the file number
        (\d+)         # file index
        (\.([a-z]+))$ # file extension
    ''',re.VERBOSE)
    
    # Method that renames the items of an array
    def rename(array):
        for i in range(len(array)):
            matchObj = pattern.search(array[i])
            temp = list(matchObj.group(2))
            temp[-1] = str(i+1)
            index = ''.join(temp)
            array[i] = matchObj.group(1) + index + matchObj.group(3)
        return(array)
    
    array = []
    directory = sorted(os.listdir('.'))
    
    for item in directory:
        matchObj = pattern.search(item)
        if not matchObj:
            continue
        if len(array) == 0 or matchObj.group(1) in array[0]:
            array.append(item)
        else:
            temp = array
            newNames = rename(temp)
            for i in range(len(temp)):
                os.rename(os.path.join(working_dir,temp[i]),
                            os.path.join(working_dir,newNames[i]))
            array.clear() #reset array for other files
            array.append(item) 
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   SwiftsNamesake    7 年前

    总之,您希望找到名称以数字结尾的每个文件 为每一组具有相同名称的文件填充空白,保留数字后缀。你 不要 想要创建任何新文件;相反,那些 数字应该用来填补空白。

    由于这个摘要很好地翻译成了代码,我将这样做,而不是脱离您的代码。

    import re
    import os
    
    from os import path
    
    folder  = 'path/to/folder/'
    pattern = re.compile(r'(.*?)(\d+)(\.[a-z]+)$')
    summary = {}
    
    for fn in os.listdir(folder):
      m = pattern.match(fn)
      if m and path.isfile(path.join(folder, fn)):
        # Create a key if there isn't one, add the 'index' to the set
        # The first item in the tuple - len(n) - tells use how the numbers should be formatted later on
        name, n, ext = m.groups()
        summary.setdefault((name, ext), (len(n), set()))[1].add(int(n))
    
    for (name, ext), (n, current) in summary.items():
      required = set(range(1, len(current)+1)) # You want these
      gaps     = required - current            # You're missing these
      superfluous = current - required         # You don't need these, so they should be renamed to fill the gaps
    
      assert(len(gaps) == len(superfluous)), 'Something has gone wrong'
    
      for old, new in zip(superfluous, gaps):
          oldname = '{name}{n:>0{pad}}{ext}'.format(pad=n, name=name, n=old, ext=ext)
          newname = '{name}{n:>0{pad}}{ext}'.format(pad=n, name=name, n=new, ext=ext)
    
          print('{old} should be replaced with {new}'.format(old=oldname, new=newname))
    

    我想这就够了。