参照了好几个范例,做了一个图形界面批量替换文件名的小程序
import os
import easygui as g
msg = "请填写一下信息"
title = "信息输入"
fieldNames = ["*文件路径","*需要替换的文件名字符","*新文件名字符"]
fieldValues = []
fieldValues = g.multenterbox(msg,title,fieldNames )
# print(fieldValues[2])
while True:
if fieldValues == None :
break
errmsg = ""
for i in range(len(fieldNames)):
option = fieldNames[i].strip()
if fieldValues[i].strip() == "" and option[0] == "*":
errmsg += ("【%s】为必填项 " %fieldNames[i])
if errmsg == "":
break
file_path = fieldValues[0]
path = file_path #输入你要更改文件的目录
originalname = fieldValues[1]
replacename = fieldValues[2]
def main1(path1):
files = os.listdir(path1) # 得到文件夹下的所有文件名称
for file in files: #遍历文件夹
if os.path.isdir(path1 + '\\' + file):
main1(path1 + '\\' + file)
else:
files2 = os.listdir(path1 + '\\')
for file1 in files2:
if originalname in file1:
n = str(path1 + '\\' + file1.replace(originalname,replacename))
n1 = str(path1 + '\\' + str(file1))
try:
os.rename(n1, n)
except IOError:
continue
main1(path)