首页 > 解决方案 > Python:PermissionError:[Errno 13] 与 windows 中的 copy_tree 函数

问题描述

我只是使用 Windows 中的函数 distutils.dir_util.copy_tree 从特定目录复制所有文件和子文件夹:

from distutils.dir_util import copy_tree
from datetime import datetime

directories_to_backup = {
    'folder_a' : 'e:\\folder_a',
}

now = datetime.now()
backup_name_folder = now.strftime("%Y%m%d-%H%M")

for name_folder, path_folder in directories_to_backup.items():
    print(f'Backuping up the folder {name_folder}')
    backup_full_path = f't:\\zzzBackup\\{backup_name_folder}\\{name_folder}\\'
    copy_tree(path_folder, backup_full_path)

它工作正常,直到它到达某个特定文件并出现以下错误:

Traceback (most recent call last):
  File "C:\Users\UserR\AppData\Local\Programs\Python\Python37\lib\distutils\file_util.py", line 48, in _copy_file_contents
    buf = fsrc.read(buffer_size)
PermissionError: [Errno 13] Permission denied

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/UserR/PycharmProjects/my_scripts/main_backup.py", line 28, in <module>
    copy_tree(path_folder, backup_full_path)
  File "C:\Users\UserR\AppData\Local\Programs\Python\Python37\lib\distutils\dir_util.py", line 159, in copy_tree
    verbose=verbose, dry_run=dry_run))
  File "C:\Users\UserR\AppData\Local\Programs\Python\Python37\lib\distutils\dir_util.py", line 159, in copy_tree
    verbose=verbose, dry_run=dry_run))
  File "C:\Users\UserR\AppData\Local\Programs\Python\Python37\lib\distutils\dir_util.py", line 159, in copy_tree
    verbose=verbose, dry_run=dry_run))
  [Previous line repeated 1 more times]
  File "C:\Users\UserR\AppData\Local\Programs\Python\Python37\lib\distutils\dir_util.py", line 163, in copy_tree
    dry_run=dry_run)
  File "C:\Users\UserR\AppData\Local\Programs\Python\Python37\lib\distutils\file_util.py", line 151, in copy_file
    _copy_file_contents(src, dst)
  File "C:\Users\UserR\AppData\Local\Programs\Python\Python37\lib\distutils\file_util.py", line 51, in _copy_file_contents
    "could not read from '%s': %s" % (src, e.strerror))
distutils.errors.DistutilsFileError: could not read from 'e:\folder_a\0\binlog': Permission denied

Process finished with exit code 1

如何更新 Windows 中的权限,以便我的脚本能够读取和复制文件?

标签: python-3.xwindowspermissions

解决方案


推荐阅读