运行此程序时出现 FileNotFoundError

时间:2020-12-31 18:58:25

标签: python python-3.x

我正在处理将文件从子文件夹移动到另一个位置以组织我的文件。这样做时,我遇到了该程序的一些错误。

import os
from shutil import move

src = "C:\\Users\\User\\Desktop\\SMALL"
dst = "C:\\ALL IN ONE\\Temporary Folder"

if not os.path.exists(dst):
    os.makedirs(dst)
for x, y, z in os.walk(src):
    for files in z:
        if files.endswith("txt"):
            move(files, dst)

当我运行程序时,它会引发一个 FileNotFoundError: [WinError 2] The system cannot find the file specified: '0.txt' -> 'C:\\ALL IN ONE\\Temporary Folder\\0.txt' 错误和一堆其他的“Traceback Errors”(如果它们就是这样称呼的话)

1 个答案:

答案 0 :(得分:0)

您需要构建源路径。尝试更改:

move(files, dst)

到:

path = os.path.join(x, files)
move(path, dst)

您可能还想将 files 重命名为 file,因为每次循环时它只是一个文件。

Justin Ezequiel 指出,在对 src 的调用中包含 os.path.join 是不正确的,因此我更新了答案以简单地加入 xfiles

相关问题