python怎么写自己的名字_用python写一个序列图改名工具

在工作中经常遇到辛苦渲染得到的序列图名字不对,怎么办!

求人不如求自己,用python写一个批量改名的工具,从此改名不求人。比如我想把名字为cat.01.jpg改为dog.001.jpg

写了半天,我终于写出来了,直接上代码:

import os

# 序列图所在的文件夹

folder = r'E:\temp\test\seq'

# 旧名字

oldName = 'cat'

# 新名字

newName = 'dog'

files = [f for f in os.listdir(folder) if os.path.isfile("%s/%s" %(folder, f)) and oldName in f]

os.chdir(folder)

for f in files:

tmp = f.split(".")

num = tmp[1].zfill(3) # 将数字部分的01改为001

new_file = "{0}.{1}.{2}".format(newName, num, tmp[2])

os.rename(f, new_file)

代码逐行解析

files = [f for f in os.listdir(folder) if os.path.isfile("%s/%s" %(folder, f)) and oldName in f]

这句也可以这样写:

files = []

# 遍历folder文件夹里的每个文件

for f in os.listdir(folder):

if os.path.isfile("%s/%s" %(folder, f)) and oldName in f:

files.append(f)

if os.path.isfile("%s/%s" %(folder, f)) and oldName in f:

这里判断f是文件而且文件名字中带oldName的才会继续。这样就找到了所有名字为oldName的文件。

os.chdir(folder)切换当前工作目录为folder。

tmp = f.split(".")

new_file = "{0}.{1}.jpg".format(newName, tmp[1])

这里将文件的名字拆分成[旧名字,数字序列部分,后缀名],然后用新名字替换旧名字重新组合出完整的新名字。os.rename这个方法可以完成文件改名.

最后一个for循环完成改名。

再来一个带界面的 (在houdini16和maya2017里都可以运行).

# coding=utf-8

import os

import sys

from PySide2 import QtGui

from PySide2 import QtCore

from PySide2 import QtWidgets

class ReNameTool(QtWidgets.QDialog):

def __init__(self):

super(ReNameTool, self).__init__()

self.setWindowTitle(u'序列改名')

main_layout = QtWidgets.QVBoxLayout(self)

folder_layout = QtWidgets.QHBoxLayout()

folder_label = QtWidgets.QLabel("folder: ")

self.folder_lineedit = QtWidgets.QLineEdit()

folder_layout.addWidget(folder_label)

folder_layout.addWidget(self.folder_lineedit)

main_layout.addLayout(folder_layout)

old_name_layout = QtWidgets.QHBoxLayout()

old_name_label = QtWidgets.QLabel("old_name: ")

self.old_name_lineedit = QtWidgets.QLineEdit()

old_name_layout.addWidget(old_name_label)

old_name_layout.addWidget(self.old_name_lineedit)

main_layout.addLayout(old_name_layout)

new_name_layout = QtWidgets.QHBoxLayout()

new_name_label = QtWidgets.QLabel("new_name: ")

self.new_name_lineedit = QtWidgets.QLineEdit()

new_name_layout.addWidget(new_name_label)

new_name_layout.addWidget(self.new_name_lineedit)

main_layout.addLayout(new_name_layout)

ok_btn = QtWidgets.QPushButton("ok")

main_layout.addWidget(ok_btn)

ok_btn.clicked.connect(self.rename_it)

def rename_it(self):

# name.01.jpg

folder = self.folder_lineedit.text()

oldName = self.old_name_lineedit.text()

newName = self.new_name_lineedit.text()

files = [f for f in os.listdir(folder) if os.path.isfile("%s/%s" % (folder, f)) and oldName in f]

os.chdir(folder)

for f in files:

tmp = f.split(".")

num = tmp[1].zfill(3)

new_file = "{0}.{1}.{2}".format(newName, num, tmp[2])

os.rename(f, new_file)

# app = QtWidgets.QApplication(sys.argv)

dialog = ReNameTool()

dialog.show()

# app.exec_()

另外推荐几个入门python的网站

https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000

http://www.runoob.com/python/python-tutorial.html

学了python写了工具还想写个漂亮的界面?学pyqt!

http://www.cnblogs.com/jakeyChen/articles/4103494.html

http://zetcode.com/gui/pyqt5/firstprograms/

https://www.jianshu.com/p/308fea12c8f8

最后写了很多代码,想多人协作外加版本控制怎么办,学git!

https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

你可能感兴趣的:(python怎么写自己的名字)