根据查找的文件重命名散装 [英] Bulk renaming of files based on lookup

查看:129
本文介绍了根据查找的文件重命名散装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个完整的图像文件的文件夹,例如

I have a folder full of image files such as


  • 1500000704_full.jpg

  • 1500000705_full.jpg

  • 1500000711_full.jpg

  • 1500000712_full.jpg

  • 1500000714_full.jpg

  • 1500000744_full.jpg

  • 1500000745_full.jpg

  • 1500000802_full.jpg

  • 1500000803_full.jpg

我需要根据由具有条目,例如,

I need to rename the files based on a lookup from a text file which has entries such as,


  • SH103239 1500000704

  • SH103240 1500000705

  • SH103241 1500000711

  • SH103242 1500000712

  • SH103243 1500000714

  • SH103244 1500000744

  • SH103245 1500000745

  • SH103252 1500000802

  • SH103253 1500000803

  • SH103254 1500000804

所以,我希望要重命名的图像文件,

So, I want the image files to be renamed,


  • SH103239_full.jpg

  • SH103240_full.jpg

  • SH103241_full.jpg

  • SH103242_full.jpg

  • SH103243_full.jpg

  • SH103244_full.jpg

  • SH103245_full.jpg

  • SH103252_full.jpg

  • SH103253_full.jpg

  • SH103254_full.jpg

我怎么可以做这个工作最容易?任何一个可以给我写一个快速的命令或脚本,可以帮我这个忙好吗?我有很多这些图像文件和手动更改的心不是可行的。

How can I do this job the easiest? Any one can write me a quick command or script which can do this for me please? I have a lot of these image files and manual change isnt feasible.

我在Ubuntu,但根据不同的工具,我可以切换到窗口如果需要的话。理想的情况是我喜欢有它在bash脚本,这样我可以了解更多信息或简单的Perl和Python。

I am on ubuntu but depending on the tool I can switch to windows if need be. Ideally I would love to have it in bash script so that I can learn more or simple perl or python.

感谢

编辑:不得不改变文件名

Had to Change the file names

推荐答案

下面是一个简单的Python脚本2做重命名。

Here's a simple Python 2 script to do the rename.

#!/usr/bin/env python

import os

# A dict with keys being the old filenames and values being the new filenames
mapping = {}

# Read through the mapping file line-by-line and populate 'mapping'
with open('mapping.txt') as mapping_file:
    for line in mapping_file:
        # Split the line along whitespace
        # Note: this fails if your filenames have whitespace
        new_name, old_name = line.split()
        mapping[old_name] = new_name

suffix = '_full'

# List the files in the current directory
for filename in os.listdir('.'):
    root, extension = os.path.splitext(filename)
    if not root.endswith(suffix):
        # File doesn't end with this suffix; ignore it
        continue
    # Strip off the number of characters that make up suffix
    stripped_root = root[:-len(suffix)]
    if stripped_root in mapping:
        os.rename(filename, ''.join(mapping[stripped_root] + suffix + extension))

脚本

各种各样位是硬codeD真的不应该。这些措施包括映射文件的名称(的mapping.txt )和文件名后缀( _full )。这些可能presumably被作为参数传入,并使用PTED间$ P $ sys.argv中

Various bits of the script are hard-coded that really shouldn't be. These include the name of the mapping file (mapping.txt) and the filename suffix (_full). These could presumably be passed in as arguments and interpreted using sys.argv.

这篇关于根据查找的文件重命名散装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆