PermissionError:[WinError 5]访问被拒绝

时间:2013-09-23 15:37:47

标签: python-3.x multiprocessing file-permissions access-denied

我是Python的初学者。我正在使用多处理来批量编程,例如从主机PC到远程PC的xcopy。我收到了许可错误,例如 * DUPLICATE_SAME_ACCESS * 。任何帮助都会非常有帮助。

IDE Env。 :

  • 开普勒
  • Python的3.x的

错误消息

Traceback (most recent call last):
  File "C:\Users\Administrator\.eclipse\org.eclipse.platform_4.3.0_1709980481_win32_win32_x86_64\plugins\org.python.pydev_2.8.2.2013090511\pysrc\pydevd.py", line 1446, in <module>
    debugger.run(setup['file'], None, None)
  File "C:\Users\Administrator\.eclipse\org.eclipse.platform_4.3.0_1709980481_win32_win32_x86_64\plugins\org.python.pydev_2.8.2.2013090511\pysrc\pydevd.py", line 1092, in run
    pydev_imports.execfile(file, globals, locals) #execute the script
  File "C:\Users\Administrator\.eclipse\org.eclipse.platform_4.3.0_1709980481_win32_win32_x86_64\plugins\org.python.pydev_2.8.2.2013090511\pysrc\_pydev_execfile.py", line 38, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc) #execute the script
  File "C:\Users\Administrator\Documents\Kepler\workspace\kr.caleb.python.first\src\fileCopy\xCopyPython.py", line 92, in <module>
    dist_files(server_list, 'C:\\Users\\Public\\jvision')
  File "C:\Users\Administrator\Documents\Kepler\workspace\kr.caleb.python.first\src\fileCopy\xCopyPython.py", line 79, in dist_files
    Process(target=multi_distribute_file,args=(lock, server, dirpath, filename, path)).start()                                
  File "C:\Python33\lib\multiprocessing\process.py", line 111, in start
    self._popen = Popen(self)
  File "C:\Python33\lib\multiprocessing\forking.py", line 248, in __init__
    dump(process_obj, to_child, HIGHEST_PROTOCOL)
  File "C:\Python33\lib\multiprocessing\forking.py", line 166, in dump
    ForkingPickler(file, protocol).dump(obj)
  File "C:\Python33\lib\multiprocessing\synchronize.py", line 70, in __getstate__
    return (Popen.duplicate_for_child(sl.handle), sl.kind, sl.maxvalue)
  File "C:\Python33\lib\multiprocessing\forking.py", line 258, in duplicate_for_child
    return duplicate(handle, Popen._tls.process_handle)
  File "C:\Python33\lib\multiprocessing\forking.py", line 201, in duplicate
    0, inheritable, _winapi.DUPLICATE_SAME_ACCESS
PermissionError: [WinError 5] Access is denied

完整代码(xCopyPython.py):

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# based on Carnival http://ask.python.kr/users/6970/carnival/

import os, sys, csv, re
from multiprocessing import Process, Lock

class Server:
    def __init__(self, addr, path):
        self.addr = addr
        self.path = path

def multi_distribute_file(lo, server, dirpath, filename, subdir):
    lo.acquire()

    pathname = os.path.join(dirpath, filename)    
    print("Read from {}".format(pathname))         

    with open(pathname, 'rb') as inFile:
        buffer = inFile.read()  

    l = re.findall(r"[\w']+",subdir)
    m = re.findall(r"[\w']+",dirpath) 

    cnt_l = len(l)
    cnt_m = len(m)

    remotepath = "//%s/%s" % (server.addr, server.path)
    if(cnt_m > cnt_l):
        for j in range(cnt_m - cnt_l):
            remotepath += "/%s" % (m[cnt_l + j])

    remotepath += "/%s" % (filename)        
    print ("Write to {}".format(remotepath))

    with open(remotepath, 'wb') as outFile:
        outFile.write(buffer)              

    lo.release() 

def make_dir(server_list, subdir, dirpath):
    for server in server_list:  
        l = re.findall(r"[\w']+",subdir)
        m = re.findall(r"[\w']+",dirpath) 

        cnt_l = len(l)
        cnt_m = len(m)

        path = "//%s/%s" % (server.addr, server.path)
        if(cnt_m > cnt_l):
            for j in range(cnt_m - cnt_l):
                path += "/%s" % (m[cnt_l + j])

        d = os.path.dirname(path)
        if not os.path.exists(d):
            os.makedirs(d)
            print ("Make dir {}".format(d))         
        else:
            print ("dir {} already exists.".format(d))

        if not os.path.exists(path):
            os.makedirs(path)
            print ("Make dir {}".format(path))          
        else:
            print ("dir {} already exists.".format(path))

def dist_files(server_list, subdir):
    for dirpath, dirnames, filenames in os.walk(subdir):           
        make_dir(server_list, subdir, dirpath) 

        for filename in filenames:  
            for server in server_list:
                lock = Lock()
                Process(target=multi_distribute_file,args=(lock, server, dirpath, filename, subdir)).start()

def get_server_list(filename):    
    mydictionary = []
    csvFile = csv.reader(open(filename, "r"))
    for row in csvFile:
      mydictionary.append(Server(row[0], row[1]))
      print("{}, {}".format(row[0], row[1]))

    return mydictionary

if __name__ == '__main__':        
    server_list = get_server_list('client_list.csv')
    dist_files(server_list, 'C:\\Users\\Public\\Test')

client.csv:

  • 192.168.10.100,测试
  • 192.168.10.101,测试
  • 192.168.10.102,测试

1 个答案:

答案 0 :(得分:0)

我要修改 multi_distribute_file 方法来打开/读取文件。

原产地:

def multi_distribute_file(lo, server, dirpath, filename, subdir):
    lo.acquire()

    pathname = os.path.join(dirpath, filename)    
    print("Read from {}".format(pathname))         

    with open(pathname, 'rb') as inFile:
        buffer = inFile.read()  

    l = re.findall(r"[\w']+",subdir)
    m = re.findall(r"[\w']+",dirpath) 

    cnt_l = len(l)
    cnt_m = len(m)

    remotepath = "//%s/%s" % (server.addr, server.path)
    if(cnt_m > cnt_l):
        for j in range(cnt_m - cnt_l):
            remotepath += "/%s" % (m[cnt_l + j])

    remotepath += "/%s" % (filename)        
    print ("Write to {}".format(remotepath))

    with open(remotepath, 'wb') as outFile:
        outFile.write(buffer)              

    lo.release() 

修改:添加缓冲区arg。

def multi_distribute_file(server, dirpath, filename, subdir, buffer):
        #lo.acquire()

        #pathname = os.path.join(dirpath, filename)    
        #print("Read from {}".format(pathname))         

        #with open(pathname, 'rb') as inFile:
        #    buffer = inFile.read()  

        l = re.findall(r"[\w']+",subdir)
        m = re.findall(r"[\w']+",dirpath) 

        cnt_l = len(l)
        cnt_m = len(m)

        remotepath = "//%s/%s" % (server.addr, server.path)
        if(cnt_m > cnt_l):
            for j in range(cnt_m - cnt_l):
                remotepath += "/%s" % (m[cnt_l + j])

        remotepath += "/%s" % (filename)        
        print ("Write to {}".format(remotepath))

        with open(remotepath, 'wb') as outFile:
            outFile.write(buffer)              

        #lo.release()