SystemWindows8.1 Python3.4
重复获取FileNotFound Errno2,试图复制目录中的所有文件。
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(源文件,destinationfile)可以很好地复制单个文件。
发布于 2016-05-19 20:34:03
os.listdir()
只列出没有路径的文件名。如果没有完整的路径,shutil.copy()
将该文件视为相对于当前工作目录的文件,并且当前工作目录中没有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\\")
所以现在shutil.copy()
被告知要复制C:\Users\Chess\events\aerofl03.pgn
,而不是<CWD>\aerofl03.pgn
。
https://stackoverflow.com/questions/37333467
复制相似问题