2016-05-19 6 views
0

システムWindows 8.1 Python 3.4
繰り返しFileNotFound Errno2を取得し、ディレクトリ内のすべてのファイルをコピーしようとしました。python 3.x shutil.copy FileNotFoundError

import os 
import shutil 
source = os.listdir("C:\\Users\\Chess\\events\\") 
for file in source : 
    shutil.copy(file, "E:\\events\\") 

利回り

FileNotFoundError : [Errno2] No such file or directory 'aerofl03.pgn'. 

'aerofl03.pgn'は、ソースリスト['aerofl03.pgn', ...]の最初のですが。

for file in source : 
    if file.endswith('.pgn') : 
     shutil.copy(file, "E:\\events\\") 

同じ結果をコード化

for file in "C:\\Users\\Chess\\events\\" : 

マイshutil.copy(sourcefileの、destinationfile)は、個々のファイルのコピーを正常に動作している場合:行が追加された場合 同じ結果。

+0

ファイルの値を確認し、完全なパスが含まれていることを確認します – Brody

答えて

2

os.listdir()パスがないファイルをリストします。フルパスが指定されていない場合、shutil.copy()はファイルを現在の作業ディレクトリに対する相対パスとして扱います。現在の作業ディレクトリにはaerofl03.pgnファイルはありません。代わりに<CWD>\aerofl03.pgnの、だから今shutil.copy()C:\Users\Chess\events\aerofl03.pgnをコピーするように言われ

path = "C:\\Users\\Chess\\events\\" 
source = os.listdir(path) 

for filename in source: 
    fullpath = os.path.join(path, filename) 
    shutil.copy(fullpath, "E:\\events\\") 

前に追加のパスが再びフルパス名を取得します。