2011-12-20 68 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()