2011-12-20 122 views
0

我想从一个位置复制一个目录到其他位置,我已经为此编写了代码。执行IDE会给我一个例外。在Python中复制目录

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 

请有人帮我摆脱了异常。

+1

使用'shutil.copytree'复制整个目录。 – 2011-12-20 07:10:36

+0

朋友最后我得到了答案给我的Quesn ;;;这很简单,我们应该调用类名称规范的变量,如“”“classname.variablename”“” – 2011-12-21 09:57:56

回答

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) 

但是你永远不会改变这些变量的值。自从你将它们初始化为相同的东西。您的复制命令正在尝试将某些内容复制到自己。 (这是错误说什么:

Error: tk_chooseDirectory and tk_chooseDirectory are the same file

您需要一种方法在你想使用,使shutil.copy()会做你想做的这两个目录进入

+0

unholysampler @请你详细说明ans。 tk_chooseDirectory用于选择目录。 – 2011-12-20 07:08:06

+0

@BharathGupta:该按钮将其用作回调函数。问题是,回调只是打开对话框,并允许您选择一个目录。它__不会将该结果保存到您碰巧用来初始化按钮的变量中。 – unholysampler 2011-12-20 13:39:54

+0

多数民众赞成这样很好。现在我怎么能存储我选择使用tk_ChooseDirectory的路径。 – 2011-12-20 13:43:27

1

Please some one help me to get rid of the exception.

一个安全可靠的方式。摆脱例外的是这样的模式:

try: 
    #shutil naughtiness 
except: 
    pass 

...但是,一个人的一定要提醒你的同事的愤怒

。 210

它看起来像在你的特定情况下来源和目的地是一样的。看起来在这种情况下最合适的做法是句柄的例外。特别是因为这只是复制的许多失败模式之一。你应该将每一个升级到用户,因为用户应该知道如何解决它。

您处于令人羡慕的位置,您的代码可能具备良好的处理异常的能力。尝试

try: 
    shutil.copy(abc, abc1) 
except Error, e: 
    tkMessageBox.showwarning(
     "Copying file", 
     "Error while copying\n(%s)" % e.msg 
    ) 
+0

尝试: shutil.copy(ABC,ABC1) 不同的是: tkMessageBox.showwarning( “复制文件”, “复制时出错\ n(%s)” ) 老兄,这是我所做的改变。在运行文件时,它给我一个错误,我应该怎么做我的目录复制到其他。 请请帮我在这方面 – 2011-12-20 08:11:16

+0

哥们,用户会看到错误并采取纠正措施。上面的异常处理程序会这样做。用户将看到“tk_chooseDirectory和tk_chooseDirectory是同一个文件”,并意识到,“糟糕!我无法将文件复制到自身上,这很愚蠢。” – 2011-12-20 14:53:20

+0

@Brain链。这是正确的,但现在如果我想复制。,我怎么可以进步,什么是做的方法。请给我一个清晰的想法。谢谢 – 2011-12-21 04:44:10

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()