首页 > 解决方案 > 复制文件时出现 FileNotFoundError

问题描述

我在 Ubuntu 18.04 上使用 python 3.8 工作,并且我有一个间歇性的shutil.copy2功能异常:

def copy_file(src, dst):
    assert os.path.exists(src), "Source file does not exist"
    assert os.path.exists(dst), "Destination folder does not exist"
    shutil.copy2(src, dst)

随机地,对于同一个文件/文件夹,此函数会在子函数中引发FileNotFoundError异常。shutil.copystat我试图直接使用shutil.copyfile,但在这里复制的文件再次随机不存在。添加一些sleep也无济于事。

在 中shutil.copystat,当函数尝试访问文件以获取其元数据时引发异常

[L372] lookup("utime")(dst, ns=(st.st_atime_ns, st.st_mtime_ns),
           follow_symlinks=follow)

就像声明的那样:

FileNotFoundError: [Errno 2] No such file or directory
../../miniconda3/envs/test_env/lib/python3.8/shutil.py:372: FileNotFoundError

标签: pythonpython-3.x

解决方案


推荐阅读