【问题标题】:Renaming files in a numerical sequence + letters以数字序列+字母重命名文件
【发布时间】:2019-07-28 04:18:37
【问题描述】:

我有一个文件夹,其中包含名为 image1.png, image2.png, image3.png 等的 .png 图像。

这个文件夹的组织方式,3 个连续的图像代表来自同一主题的数据,所以我希望它们用相同的识别号 + 一个字母来命名以区分它们。像这样:

image1.png --> 1-a.png
image2.png --> 1-b.png
image3.png --> 1-c.png
image4.png --> 2-a.png
image5.png --> 2-b.png
image6.png --> 2-c.png

等等。

最好的方法是什么? Perl 脚本?或在python 中生成具有所需名称的.txt,然后使用它重命名文件?我正在使用Ubuntu

提前致谢!

【问题讨论】:

  • 几乎任何脚本语言都足够了。为什么不试一试,看看你们相处得怎么样?
  • 脚本也应该处理排序逻辑,或者你可以让它按特定顺序排序?因为我们不知道文件名模式到底是什么。您在问题中提到了image01image1
  • @Kent 文件已经排序,没有前导零。感谢您指出这一点 - 我刚刚编辑了问题。

标签: python bash perl ubuntu renaming


【解决方案1】:

我在 python3 中试过这个,它可以根据你的需要工作。

import os
import string
os.chdir(path_to_your_folder)
no=1
count=1
alphabet = ['a','b','c']
count=0
for filename in os.listdir(os.getcwd()):
    newfilename = (str(no) + "-" + alphabet[count]+".png")
    os.rename(filename,newfilename)
    count=(count+1)%3
    if count==0:
        no=no+1

【讨论】:

    【解决方案2】:

    有趣的项目。 :)

    declare -i ctr=1 fileset=1              # tracking numbers
    declare -a tag=( c a b )                # lookup table
    for f in $( printf "%s\n" *.png |       # stack as lines
                sort -k1.6n )               # order appropriately
    do ref=$(( ctr++ % 3 ))                 # index the lookup
       end=${f##*.}                         # keep the file ending
       mv "$f" "$fileset-${tag[ref]}.$end"  # mv old file to new name
       (( ref )) || (( fileset++ ))         # increment the *set*
    done
    
    mv image1.png 1-a.png
    mv image2.png 1-b.png
    mv image3.png 1-c.png
    mv image4.png 2-a.png
    mv image5.png 2-b.png
    mv image6.png 2-c.png
    mv image7.png 3-a.png
    mv image8.png 3-b.png
    mv image9.png 3-c.png
    mv image10.png 4-a.png
    mv image11.png 4-b.png
    mv image12.png 4-c.png
    

    显然,根据需要进行编辑以进行路径 &c。

    【讨论】:

      【解决方案3】:

      perl 你可以这样做:

      my @new_names = map {
          my ( $n ) = $_ =~ /image(\d+)\.png/;
          my $j = (($n - 1)  % 3) + 1;
          my $char = (qw(a b c))[ int( $n / 3 ) ];
          "$j-$char.png"
      } @files;
      

      这假定@files 数组没有特殊顺序。

      【讨论】:

        【解决方案4】:

        给定 Python 中的文件列表 files,您可以使用 itertools.product 生成所需的数字和字母配对:

        from itertools import product
        import os
        
        os.chdir(directory_containing_images)
        # instead of hard-coding files you will actually want:
        # files = os.listdir('.')
        files = ['image1.png', 'image2.png', 'image3.png', 'image4.png', 'image5.png', 'image6.png']
        for file, (n, a) in zip(files, product(range(1, len(files) // 3 + 2), 'abc')):
            os.rename(file, '{}-{}.png'.format(n, a))
        

        如果将os.rename替换为print,上面的代码会输出:

        image1.png 1-a.png
        image2.png 1-b.png
        image3.png 1-c.png
        image4.png 2-a.png
        image5.png 2-b.png
        image6.png 2-c.png
        

        【讨论】:

        • 我最终选择了这个解决方案。我必须添加人工/自然排序来解决订单问题,例如image1.png 后面跟着image10.png。这个问题对此很有帮助:stackoverflow.com/questions/5967500/…
        猜你喜欢
        • 2020-01-16
        • 2017-11-13
        • 2019-02-28
        • 1970-01-01
        • 2021-03-06
        • 2013-06-03
        • 2020-12-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多