【问题标题】:FileNotFoundError when attempting to move files into a temp directory尝试将文件移动到临时目录时出现 FileNotFoundError
【发布时间】:2019-12-16 17:42:25
【问题描述】:

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

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

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

【问题讨论】:

    标签: python file shutil temporary-directory


    【解决方案1】:

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

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多