2012-04-06 36 views
4

我一直在嘗試使用「copytree(src,dst)」,但是因爲目標文件夾應該存在於all.Here你可以看到一小段代碼,我寫道:如何從c:\ xxx yyy複製目錄到c:\ zzz in python

def copy_dir(src,dest): 
    import shutil 
    shutil.copytree(src,dest) 

copy_dir('C:/crap/chrome/','C:/test/') 

,這是我爲得到我預期的錯誤...

Traceback (most recent call last): 
File "C:\Documents and Settings\Administrator\workspace\MMS-Auto\copy.py", line 5, in  <module> 
copy_dir('C:/crap/chrome/','C:/test/') 
File "C:\Documents and Settings\Administrator\workspace\MMS-Auto\copy.py", line 3, in copy_dir 
shutil.copytree(src,dest) 
File "C:\Python27\lib\shutil.py", line 174, in copytree 
os.makedirs(dst) 
File "C:\Python27\lib\os.py", line 157, in makedirs 
mkdir(name, mode) 
WindowsError: [Error 183] Cannot create a file when that file already exists: 'C:/test/' 

這裏是我的問題是否有辦法在不創建我自己的copytree函數的情況下實現相同的結果?

預先感謝您。

+0

那你想幹什麼?覆蓋文件?如果文件已存在,請保留原始位置? – marue 2012-04-06 18:31:58

+0

就足夠了,只是將它們複製過來,但是我可能想在稍後添加這樣的功能 – nassio 2012-04-06 18:34:02

回答

1

看看errno可能的錯誤。您可以先使用.copytree(),然後在出現錯誤時使用shutil.copy

來源:http://docs.python.org/library/shutil.html#shutil.copytree

If exception(s) occur, an Error is raised with a list of reasons.

,那麼你可以決定如何處理它,並實現你的代碼來處理它。

import shutil, errno 

def copyFile(src, dst): 
    try: 
     shutil.copytree(src, dst) 
    # Depend what you need here to catch the problem 
    except OSError as exc: 
     # File already exist 
     if exc.errno == errno.EEXIST: 
      shutil.copy(src, dst) 
     # The dirtory does not exist 
     if exc.errno == errno.ENOENT: 
      shutil.copy(src, dst) 
     else: 
      raise 

關於.copy()http://docs.python.org/library/shutil.html#shutil.copy

Copy the file src to the file or directory dst. If dst is a directory, a file with the same basename as src is created (or overwritten) in the directory specified. Permission bits are copied. src and dst are path names given as strings.

編輯:也許還考慮distutils.dir_util.copy_tree

+0

裏面的if語句你沒有調用任何東西......我應該在那裏添加什麼? – nassio 2012-04-06 18:53:48

+0

好的謝謝,無論如何,我得到「WindowsError:[錯誤183]該文件已存在時不能創建文件:'C:/ test /'」 – nassio 2012-04-06 19:03:00

+0

編輯:如果文件存在'EEXIST',然後使用'.copy ()',它應該覆蓋它們。 – George 2012-04-06 19:05:08

0

我敢肯定,這取決於你有的Python的確切版本,但是當我打電話給shutil.copytree。 DOC我得到這個:

Recursively copy a directory tree using copy2().

The destination directory must not already exist.

XXX Consider this example code rather than the ultimate tool.

這說明你所得到的錯誤。您可以改用distutils.dir_util.copy_tree

+0

我不想向python本身添加任何東西,但如果我不能這樣做,我會去的 – nassio 2012-04-06 18:57:22

+0

你的意思是你不想導入任何圖書館?你導入shutil,你可以寫'輸入distutils'。 – marue 2012-04-06 19:01:12

+0

對不起,我誤讀了Python的文檔,謝謝它的工作......再次感謝 – nassio 2012-04-06 19:31:00

1

我有一個小的工作圍繞檢查是否有在同一名稱的目錄在執行shutil.copytree函數之前先定位。它也只是複製具有特定通配符的目錄。不知道是否需要回答這個問題,但我想把它留在那裏。

import sys 
import os 
import os.path 
import shutil 


src="/Volumes/VoigtKampff/Temp/_Jonatha/itmsp_source/" 
dst="/Volumes/VoigtKampff/Temp/_Jonatha/itmsp_drop/" 


for root, dirs, files in os.walk(src): 
    for dir in dirs: 
     if dir.endswith('folder'): 
      print "directory to be moved: %s" % dir 
      s = os.path.join(src, dir) 
      d = os.path.join(dst, dir) 
      if os.path.isdir(d): 
       print "Not copying - because %s is already in %s" % (dir, dst) 
      elif not os.path.isdir(d): 
       shutil.copytree(s, d) 
       print "Copying %s to %s" % (dir, dst) 
7

我使用distutils包比在這裏的其他答案更大的成功。

http://docs.python.org/2/distutils/apiref.html#module-distutils.dir_util

distutils.dir_util.copy_tree功能的工作原理非常相似shutil.copytree除了dir_util.copy_tree只是簡單地覆蓋存在的,而不是因出現異常崩潰的目錄。

替換:

import shutil 
shutil.copytree(src, dst) 

有:

import distutils.dir_util 
distutils.dir_util.copy_tree(src, dst) 
相關問題