获取'PermissionError:[error 13]使用时权限被拒绝shutil.copyfiles文件

2024-09-29 19:09:18 发布

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

我正在使用以下代码将文件夹PStool的内容复制到文件夹c:/WINDOWS/System32。我以管理员的身份运行这个程序

import shutil

import os

programSourcePath = "C:\Users\Admin\Desktop\Utilities_Installers_new\Programs"

pstoolfiles = os.listdir(programSourcePath + '/PSTools')

for name in pstoolfiles:
        srcname = os.path.join(programSourcePath + '/PSTools', name)
        shutil.copyfile(srcname, r'c:/WINDOWS/System32')

获取

PermissionError: [error 13] permission denied : 'c:/WINDOWS/System32'

Tags: 代码nameimport文件夹内容oswindows管理员
1条回答
网友
1楼 · 发布于 2024-09-29 19:09:18

copyfile()目标应该是完整的文件名。根据Python文件:

shutil.copyfile(src, dst)

Copy the contents (no metadata) of the file named src to a file named dst.
dst must be the complete target file name; look at shutil.copy() for a copy 
that accepts a target directory path. 

试试这个:

import shutil
import os

programSourcePath = "C:\Users\Admin\Desktop\Utilities_Installers_new\Programs"

pstoolfiles = os.listdir(programSourcePath + '/PSTools')

for name in pstoolfiles:
    srcname = os.path.join(programSourcePath + '/PSTools', name)
    dstname = os.path.join(r'c:/WINDOWS/System32', name)
    shutil.copyfile(srcname, dstname)

相关问题 更多 >

    热门问题