繁体   English   中英

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

[英]Python: Retrieving and renaming indexed files in a directory

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

例如,如果目录中包含以下文件>>(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) 

总而言之,您要查找名称以数字结尾的每个文件,并为每个具有相同名称的文件集填充空格,并保留数字后缀。 希望创建任何新文件; 相反,应该使用数字最大的数字来填补空白。

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

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

我认为那涵盖了它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM