python 3.x版shutil.复制文件未找到

2024-10-01 15:40:01 发布

您现在位置:Python中文网/ 问答频道 /正文

系统Windows 8.1 Python 3.4
重复get FileNotFound Errno2,试图复制目录中的所有文件。在

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

收益率

^{pr2}$

尽管'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.复制(sourcefile,destinationfile)可以精确复制单个文件。在


Tags: 文件inimportsourceforos系统events
1条回答
网友
1楼 · 发布于 2024-10-01 15:40:01

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,而不是{}。在

相关问题 更多 >

    热门问题