Python shutil.copy权限错误窗口

2024-05-03 14:16:46 发布

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

我知道这个问题在这个网站的很多地方都有被问到和回答,但没有一个解决方案对我有效。基本上,我需要shutil.copy的工作方式与shutil.move的工作方式相同(它确实如此),但是当我执行.copy时,我在源文件上得到错误13权限错误

我无法执行shutil.copytree,因为我不需要复制第一个文件夹的内容,而需要复制文件夹本身(其中包含内容)。我试过检查权限,修改,但没有效果

我也尝试过使用包含文件夹名称的完整路径(没有变量),但仍然会出现相同的错误

这是在Windows7和8.1中实现的

代码如下:

import os
import csv
import re
from os import path
from distutils.dir_util import copy_tree
import shutil


# Open the csv file
f = open("Consumers.csv")
csv_f = csv.reader(f)


#Loop throught the csv file
for eachrow in csv_f:
   
    #loop through the folders in the directory
     for foldname in os.listdir():

        #If the folder name is the same as a field in the first column of the csv file 
            if (foldname == eachrow[0]):
                
                #Name the second column filed name "bucket."  
                #This is also the name of a folder in the same directory
                bucket = eachrow[1]
                
                #copy the first folder (and contents) into the second folder
                shutil.copy (foldname, bucket)
            

错误是:


PermissionError                           Traceback (most recent call last)
<ipython-input-36-61e009418603> in <module>
     25 
     26                 #copy the first folder (and contents) into the second folder
---> 27                 shutil.copy (foldname, bucket)
     28 
     29 

~\Anaconda3\lib\shutil.py in copy(src, dst, follow_symlinks)
    243     if os.path.isdir(dst):
    244         dst = os.path.join(dst, os.path.basename(src))
--> 245     copyfile(src, dst, follow_symlinks=follow_symlinks)
    246     copymode(src, dst, follow_symlinks=follow_symlinks)
    247     return dst

~\Anaconda3\lib\shutil.py in copyfile(src, dst, follow_symlinks)
    118         os.symlink(os.readlink(src), dst)
    119     else:
--> 120         with open(src, 'rb') as fsrc:
    121             with open(dst, 'wb') as fdst:
    122                 copyfileobj(fsrc, fdst)

PermissionError: [Errno 13] Permission denied: 'Last1_First1_11111'

任何帮助都将不胜感激


Tags: csvthepathinimportsrcos错误
1条回答
网友
1楼 · 发布于 2024-05-03 14:16:46

我曾经做过类似的事情。所以如果我理解正确的话,这可能会有所帮助

您可以通过ignore=选项修改copytree的行为ignore=接受一个可调用函数,该函数依次接受当前(即递归期间)访问的路径和该路径内容的列表作为参数。它必须返回一个copytree然后忽略的iterable

我使用了以下导入(您可以调整):

import shutil
import os

将此视为忽略函数:

def ignore(path, content_list):
    return [
        content
        for content in content_list
        if os.path.isdir(os.path.join(path, content))
    ]

它将访问路径的所有子目录打包到忽略列表中。其效果是copytree在第一个目录和其中的文件之后停止其递归

而不是

shutil.copy (foldname, bucket)

使用

shutil.copytree(foldname, bucket, ignore=ignore)

它是为我设计的。但我有点不确定你的路径结构到底是什么样子。如果foldnamebucket绝对路径就好了

为了确保将以下内容添加到导入中(Path类使使用路径变得非常简单):

from pathlib import Path

然后用

    #loop through the folders in the directory
     for foldname in os.listdir():
         fold_path = Path(foldname).resolve()

和使用(代替上述版本)

shutil.copytree(fold_path, (fold_path.parent / bucket),
                ignore=ignore,
                dirs_exist_ok=True)

这就是我对你想要达到的目标的理解。(用小例子说明问题总是好的,以确保你的意图非常清楚。)

编辑:完成程序(无本部分不需要的注释和导入):

import csv
import os
import shutil
from pathlib import Path

def ignore(path, content_list):
    return [
        content
        for content in content_list
        if os.path.isdir(os.path.join(path, content))
    ]

f = open("Consumers.csv")
csv_f = csv.reader(f)

for eachrow in csv_f:
    for foldname in os.listdir():
        fold_path = Path(foldname).resolve()
        if foldname == eachrow[0]:
            bucket = eachrow[1]
            shutil.copytree(fold_path, 
                            (fold_path.parent / bucket),
                            ignore=ignore,
                            dirs_exist_ok=True)

相关问题 更多 >