我正在尝试将目录从一个位置复制到其他位置,并且我已经为此编写了代码。在执行IDE时给了我一个例外。
import sys
import os
from Tkinter import *
from tkCommonDialog import Dialog
import shutil
import tkFileDialog
import win32com.client
win = Tk()
win.title("Copying the Dorectory to specified location")
win.geometry("600x600+200+50")
win.resizable()
class Copy:
def __init__(self,Obj):
la = Label(win, text = "Source Directory is",font = "verdana 12 italic bold", width = 20,fg = "Red", bg = "WHITE", )
la.grid(row=1, column =1)
abc = "tk_chooseDirectory"
bu = Button(text="Source", font = "Verdana 12 italic", command= abc )
bu.grid(row =1 , column =3)
la1 = Label(win, text = "DestibationDirectory is",font = "verdana 12 italic bold", width = 20,fg = "Red", bg = "WHITE", )
la1.grid(row=2, column =1)
abc1 = "tk_chooseDirectory"
bu1 = Button(text="Destination", font = "Verdana 12 italic", command=abc1)
bu1.grid(row =2 , column =3)
def start():
shutil.copy(abc, abc1)
bu2 = Button(text="Copy", font= "Verdana 12 bold", command =start)
bu2.grid(row =3, column =2)
obj = Copy(win)
win.mainloop()
这是我的代码,我面临的例外是
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
return self.func(*args)
File "C:\Documents and Settings\Bharath Gupta\Desktop\task.py", line 38, in start
shutil.copy(abc, abc1)
File "C:\Python27\lib\shutil.py", line 116, in copy
copyfile(src, dst)
File "C:\Python27\lib\shutil.py", line 68, in copyfile
raise Error("`%s` and `%s` are the same file" % (src, dst))
Error: `tk_chooseDirectory` and `tk_chooseDirectory` are the same file
请有人帮助我摆脱异常。
答案 0 :(得分:1)
查看您的代码,并添加一些评论。
class Copy:
def __init__(self,Obj):
la = Label(win, text = "Source Directory is",font = "verdana 12 italic bold", width = 20,fg = "Red", bg = "WHITE", )
la.grid(row=1, column =1)
#SET abc HERE
abc = "tk_chooseDirectory"
bu = Button(text="Source", font = "Verdana 12 italic", command= abc )
bu.grid(row =1 , column =3)
la1 = Label(win, text = "DestibationDirectory is",font = "verdana 12 italic bold", width = 20,fg = "Red", bg = "WHITE", )
la1.grid(row=2, column =1)
#SET abc1 HERE
abc1 = "tk_chooseDirectory"
bu1 = Button(text="Destination", font = "Verdana 12 italic", command=abc1)
bu1.grid(row =2 , column =3)
def start():
#RUN WITH abc AND abc1
shutil.copy(abc, abc1)
但是你永远不会改变那些变量的值。因为你把它们初始化为同样的东西。您的复制命令正在尝试将某些内容复制到自身。 (这就是错误所说的:
错误:
tk_chooseDirectory
和tk_chooseDirectory
是同一个文件
您需要一种方法来输入您想要使用的两个目录,以便shutil.copy()
能够达到您想要的效果。
答案 1 :(得分:1)
请有人帮助我摆脱异常。
摆脱异常的一种可靠方法是这种模式:
try:
#shutil naughtiness
except:
pass
......但是那个肯定会引起同事的愤怒。
看起来在您的特定情况下,源和目标是完全相同的。在这种情况下,最合适的做法似乎是处理异常。特别是因为这只是复制的许多失败模式中的一种。您应该将每个用户升级为用户,因为用户应该知道如何解决它。
您处于令人羡慕的位置,您的代码可能已经准备好处理异常。试试
try:
shutil.copy(abc, abc1)
except Error, e:
tkMessageBox.showwarning(
"Copying file",
"Error while copying\n(%s)" % e.msg
)
答案 2 :(得分:0)
import sys
import os
import tkMessageBox
from Tkinter import *
from tkCommonDialog import Dialog
import shutil
import tkFileDialog
import win32com.client
win = Tk()
win.title("Copying the Directory to specified location")
win.geometry("600x600+200+50")
win.resizable()
class Copy(object):
def __init__(self):
def srce():
self.src = tkFileDialog.askdirectory(title="The source folder is ")
textboxsrc.delete(0, END)
textboxsrc.insert(0, self.src)
print self.src
return self.src
textboxsrc = Entry(win, width="70")
textboxsrc.insert(0, 'Enter master file name')
textboxsrc.pack()
textboxsrc.place(relx=0.40, rely=0.06, anchor=CENTER)
bu = Button(text="Source", font="Verdana 12 italic bold", bg="Purple", fg="white", command=srce)
bu.pack(fill=X, expand=YES)
bu.place(relx=0.85, rely=0.06, anchor=CENTER)
def dest():
self.des = tkFileDialog.askdirectory(title="TheDestination folder is ")
textboxdes.delete(0, END)
textboxdes.insert(0, self.des)
print self.des
return self.des
textboxdes = Entry(win, width="70")
textboxdes.insert(0, 'Enter master file name')
textboxdes.pack()
textboxdes.place(relx=0.40, rely=0.13, anchor=CENTER)
bu1 = Button(text="Destination", font="Verdana 12 italic", bg="Purple", fg="white", command=dest)
bu1.pack(fill=X, expand=YES)
bu1.place(relx=0.85, rely=0.13, anchor=CENTER)
def start():
try:
shutil.copytree(self.src, self.des)
except :
tkMessageBox.showwarning("Copying file", "Error while copying\n(%s)")
bn = Button(text="Copy", font="Verdana 12 italic", bg="Purple", fg="white", command=start)
bn.pack(fill=X, expand=YES)
bn.place(relx=0.50, rely=0.25, anchor=CENTER)
obj = Copy()
#obj.source(win)
#obj.destination(win)
win.mainloop()