2010-12-01 13 views
3

現在のフォルダにあるすべてのファイルとフォルダをサブディレクトリにコピーする必要があります。そうする最良の方法は何でしょうか?次のスニペットを試しましたが、コピー先ディレクトリが既に存在する場合は失敗します。サブディレクトリへの再帰的ファイルコピー

def copy(d=os.path.curdir): 
    dest = "t" 
    for i in os.listdir(d): 
     if os.path.isdir(i): 
      shutil.copytree(i, dest) 
     else: 
      shutil.copy(i, dest) 

私は同じタスクをより簡単にやり遂げることができると感じています。どうすればいいのですか?

+0

は、空の先のサブフォルダですか? –

+0

スニペットは完全に間違っています。 1)ルートディレクトリではなくサブディレクトリ上で 'copytree 'を実行する理由は何ですか? 2) 'd'、' dest'は使われません。 – khachik

+0

@khachik申し訳ありませんが、急いで入力しました。 [2]世話をした。 –

答えて

1

http://docs.python.org/library/shutil.htmlのコードを参照して、少し調整してください(例:os.makedirs(dst)の周りなど)。

+0

うーん、私はこれを避けたいと思っていました。他の方法はありません。 –

0

本当にPythonを使用する必要がありますか? shutil関数はすべてのファイルメタデータとグループ権限をコピーできないためです。 Linuxの場合はcp、Windowsの場合はxcopyなどの組み込みOSコマンドを試してみてください。

あなたも、このことができます願ってい

import os 

os.system("cp file1 file2") 

のpythonからこれらのコマンドを実行しようとすることができます。

+0

Windowsの場合 – khachik

+0

os.systemを使用していますか?これを使用することに頼ると仮定すると、bashパイプを使用する必要があり、コードプラットフォームに依存します。 –

+0

@khachik:Windowsの 'xcopy'コマンドを使うことができました @ zubin71:ちょっと別の解決策です。私は、' cp'と 'xcopy'はファイルやディレクトリのコピーをより良くすることができると思います。 – Max

2

私は決してそれをpythonでやっていませんが、以下の解決策が思い浮かんできました。これは単純に見えないが、それは動作するはずですし、簡素化することができます(チェックしていない、申し訳ありませんが、コンピュータへのアクセスなしになりました):

def copyDirectoryTree(directory, destination, preserveSymlinks=True): 
    for entry in os.listdir(directory): 
    entryPath = os.path.join(directory, entry) 
    if os.path.isdir(entryPath): 
     entrydest = os.path.join(destination, entry) 
     if os.path.exists(entrydest): 
     if not os.path.isdir(entrydest): 
      raise IOError("Failed to copy thee, the destination for the `" + entryPath + "' directory exists and is not a directory") 
     copyDirectoryTree(entrypath, entrydest, preserveSymlinks) 
     else: 
     shutil.copytree(entrypath, entrydest, preserveSymlinks) 
    else: #symlinks and files 
     if preserveSymlinks: 
     shutil.copy(entryPath, directory) 
     else: 
     shutil.copy(os.path.realpath(entryPath), directory) 
1

がmamnunの答えを拡張するために、必要に応じて

osの直接呼び出しを使用するには、ディレクトリの再帰的なコピーが必要なので、私はcp -rを使うことをお勧めします。ここで

0

のpythonのための再帰的なコピー方法の私のバージョンで、動作しているようです:)あなたはコピーを開始するとき

def copy_all(fr, to, overwrite=True): 
    fr = os.path.normpath(fr) 
    to = os.path.normpath(to) 

    if os.path.isdir(fr): 
    if (not os.path.exists(to + os.path.basename(fr)) and not 
    os.path.basename(fr) == os.path.basename(to)): 
     to += "/" + os.path.basename(fr) 
     mkdirs(to) 
    for file in os.listdir(fr): 
     copy_all(fr + "/" + file, to + "/") 
    else: #symlink or file 
    dest = to 
    if os.path.isdir(to): 
     dest += "/" 
     dest += os.path.basename(fr) 

    if overwrite and (os.path.exists(dest) or os.path.islink(dest) 
     rm(dest) 

    if os.path.isfile(fr): 
     shutil.copy2(fr, dest) 
    else: #has to be a symlink 
     os.symlink(os.readlink(fr), dest) 


def mkdirs(path):             
    if not os.path.isdir(path): 
    os.makedirs(path) 


def rm(path):              
    if os.path.isfile(path) or os.path.islink(path): 
    os.remove(path) 
    elif os.path.isdir(path): 
    for file in os.listdir(path): 
     fullpath = path+"/"+file 
     os.rmdir(fullpath)