Created
February 19, 2013 07:28
-
-
Save beordle/4983783 to your computer and use it in GitHub Desktop.
给同学开网店写的文件批量改名程序 python+Tkinter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding:utf-8 | |
from Tkinter import * | |
import tkSimpleDialog,tkFileDialog,os | |
from ttk import * | |
def ask(): | |
a = tkSimpleDialog.askstring('Beordle','Text from:',initialvalue='') | |
if a==None: | |
return None,None | |
b = tkSimpleDialog.askstring('Beordle','Text to:',initialvalue='') | |
return a,b | |
class Application(Frame): | |
path="" | |
def rename(self): | |
if self.path=="": | |
print u'你还未指定目录' | |
return | |
dirname=self.path | |
print '注意,你将修改的目录为',dirname | |
while True: | |
a,b=ask() | |
if a==None: | |
return | |
print a,b | |
filenames = os.listdir(dirname) | |
for filename in filenames: | |
filename=unicode(filename) | |
if os.path.isfile(os.path.join(dirname,filename)): | |
newfilename=filename.replace(a,b) | |
filename=os.path.join(dirname,filename) | |
newfilename=os.path.join(dirname,newfilename) | |
os.rename(filename,newfilename) | |
if filename != newfilename: | |
print filename ,"to",newfilename | |
def setpath(self): | |
dirname = tkFileDialog.askdirectory(parent=root) | |
dirname=unicode(dirname) | |
print dirname | |
self.path=dirname | |
def createWidgets(self): | |
self.QUIT = Button(self) | |
self.QUIT["text"] = u"设置文件夹" | |
self.QUIT["command"] = self.setpath | |
self.QUIT.pack({"side": "left"}) | |
self.hi_there = Button(self) | |
self.hi_there["text"] = u"更名", | |
self.hi_there["command"] = self.rename | |
self.hi_there.pack({"side": "left"}) | |
def __init__(self, master=None): | |
Frame.__init__(self, master) | |
self.pack() | |
self.createWidgets() | |
root = Tk() | |
app = Application(master=root) | |
app.mainloop() | |
root.destroy() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment