0

我正在尝试将子目录中的文件移动到临时文件夹中。这是我的代码:

with tempfile.TemporaryDirectory() as tempDirectory:
   for root, dirs, files in os.walk(fileDestination, topdown=True):
        for file in files:
            shutil.move(file, tempDirectory)

当我查看调试器时,我可以看到文件变量的值,其中包含我要移动的文件。但是什么都没有移动,然后我收到错误 FileNotFoundError ,它引用了我要移动的文件。当我查看我的文件资源管理器时,我可以看到文件没有移动。

4

1 回答 1

0

我自己想出来的。因此,即使文件变量保存了文件名,它也没有保存文件的整个路径。以下代码有效:

for subdir, dirs, files in os.walk(fileDestination, topdown=True):
        for file in files:
            fileName = os.path.join(fileDestination+"\\"+file)
            print(fileName)
            shutil.move(fileName, tempDirectory)
于 2019-12-16T17:55:52.747 回答