我需要使用txt文件中包含的字符串重命名png文件。
我有一个名为的png文件(唯一改变的单词是大小):
Event_Post_260x200_CITY.png
和我写过的1个txt文件:
Place_Event,Date_Event
2018年9月10日的测试。
txt文件具有此格式,因为我在Photoshop中使用了脚本。
如何使用txt文件重命名png文件,如:
Event_Post_260x200_Test.png
我尝试了不同的方法,但我无法实现这一目标。
我正在使用Windows 10。
感谢您的帮助。
@powershell -command get-childitem *.png | foreach { rename-item $_ $_.Name.Replace("CITY", "TEST") }
答案 0 :(得分:0)
安装python后,这应该可以帮到你。内联有一些评论,你可以在哪里改变。还有explenation代码在那个特定点为你做的。
将代码复制到filename.py并从命令行运行。
...来自命令行:python remane_filenames.py
享受转换; - )
#!/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