根据txt文件中的字符串重命名部分png文件名 [英] Rename part of png file names based on a string in a txt file

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

问题描述

我需要使用txt文件中包含的字符串来重命名png文件.

I need to rename png files using a string contained in a txt file.

我有一个名为的png文件(唯一更改的单词是大小):

I have png files named like (the only words that change are the sizes):

Event_Post_260x200_CITY.png

Event_Post_260x200_CITY.png

和我写过的1个txt文件:

and 1 txt file in which I have written:

Place_Event,Date_Event

Place_Event,Date_Event

测试,2018年9月10日.

Test,10 September 2018.

txt文件具有这种格式,因为我在Photoshop中使用了脚本.

The txt file has this formatting because I use a script in Photoshop.

如何使用txt文件来重命名png文件?

How can I rename the png files using the txt file like:

Event_Post_260x200_Test.png

Event_Post_260x200_Test.png

我尝试了不同的方法,但我无法实现.

I tried different ways but I am not able to achieve that.

我正在使用Windows 10.

I'm working on Windows 10.

谢谢您的帮助.

@powershell -command get-childitem *.png | foreach { rename-item $_ $_.Name.Replace("CITY", "TEST") }

推荐答案

当您安装python时,这应该为您完成工作.内联有为您提供注释的地方,您可以在其中进行更改.在特定的时刻,代码还能为您提供帮助.

This should do the job for you when you got python installed. Inline there are comments for you where you can change things. There is also explenation what the code does for you at that particular point.

将代码复制到filename.py并从命令行运行它.

Copy the code to a filename.py and run it from the command-line.

...从命令行: python remane_filenames.py

  1. 可以使用记事本和IDLE或Komodo编辑器等编辑器打开文件.
  2. 您可以编辑变量以更改城市,日期,event_place/name等.
  3. 选择我的答案并对其进行投票,这样我的工作就不仅仅是从沉没的代码中清除灰尘.

享受转换;-)

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# remane_filenames.py

import os, shutil

print os.path.realpath(__file__)      # path where file.py is executed from.

myfilelist = os.listdir('.')           # creates filelist. The '.' can be another folder.
destfolder = "D:\output"               # target folder (may created automatically.
ftype      = 'png'

# variables you can change yourself 
Place_event = 'Windmils'
Date_event  = '2018-01-26'
City        = None

# main code:

for scr in myfilelist:                # steps through a list of files.

    if os.path.isfile(scr) == True:   # checks if file is file and not a folder.

#        print item                    # shows the filename on which we work.

        myfile = scr.split('.')

        if myfile[1] == ftype:         # correct filetype?

            try:
                event, post, size, city = myfile[0].split("_")

                print event, post, size, city

                if City == None:

                    City = city            #use default cityname

                filename = Place_event + '_' + post + '_' + Date_event + '_' + size + '_' + City + "." + ftype

                print filename

                dest = os.path.join(destfolder, filename)

                print dest

                # if the folder doesn't exist..create it.
                if os.path.isdir(destfolder) == False:
                    os.mkdir(destfolder)

                shutil.copy2(scr, dest)

            except Exception as e:
#                print e                # print line for debugging code to see if something is not working.
                continue

这篇关于根据txt文件中的字符串重命名部分png文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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