使用Python Shuti将文件复制到多个目录中

2024-10-03 17:27:05 发布

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

我试图使用shutil.copytree将一个目录复制到多个其他目录中。我不能让它工作。我很确定我只需要实现ignore_errors=True,但是我不能让它工作。我应该如何将“ignore_errors=True”实现到

for CopyHere in DeleteThis:
    for CopyThis in FilestoCopy:
        shutil.copytree(CopyThis, CopyHere)
        print('Files have been copied')

我的代码如下:

^{pr2}$

以下是我收到的错误消息:

Traceback (most recent call last):
  File "C:\Users\2402neha\OneDrive\Python\Dis Cleaner\Copy paste test.py", line 17, in <module>
    shutil.copytree(CopyThis, CopyHere)
  File "C:\Users\2402neha\AppData\Local\Programs\Python\Python35\lib\shutil.py", line 309, in copytree
    os.makedirs(dst)
  File "C:\Users\2402neha\AppData\Local\Programs\Python\Python35\lib\os.py", line 241, in makedirs
    mkdir(name, mode)
PermissionError: [WinError 5] Ingen tilgang: 'E:'

Tags: inpy目录trueforlineusersappdata
1条回答
网友
1楼 · 发布于 2024-10-03 17:27:05

您的目的地是E:

目标目录不需要存在。在

来自^{}的文档:

shutil.copytree(src, dst, symlinks=False, ignore=None)
Recursively copy an entire directory tree rooted at src. The destination directory, named by dst, must not already exist; it will be created as well as missing parent directories.

您可能需要复制的目录名并将其与目标连接:

directory = os.path.basename(CopyThis)
destination = os.path.join(CopyHere, directory)
shutil.copytree(CopyThis, destination)

相关问题 更多 >