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

Python: Retrieving and renaming indexed files in a directory

我创建了一个脚本来重命名给定目录中的索引文件

例如如果该目录有以下文件>> (bar001.txt, bar004.txt, bar007.txt, foo2.txt, foo5.txt, morty.dat, rick.py).我的脚本应该能够重命名 'only' 索引文件并像这样缩小差距 >> (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) 

总而言之,您想找到名称以数字结尾的每个文件并且 为每组具有相同名称的文件填充空白,除了数字后缀。您不想创建任何新文件;相反,应使用 最高 数字的数字来填补空白。

由于此摘要可以很好地转换为代码,因此我将这样做而不是处理您的代码。

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))

我想差不多就涵盖了。