PermissionError: [WinError 5] Отказано в доступе

Я новичок в Python. Я использую многопроцессорную обработку для пакетной программы, такой как xcopy, с хост-компьютера на удаленные ПК. Я получил ошибку разрешения, такую ​​как *DUPLICATE_SAME_ACCESS*. любая помощь будет очень полезна.

Оболочка IDE. :

  • Кеплер
  • 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, Тест

person Changju.rhee    schedule 23.09.2013    source источник


Ответы (1)


Я собираюсь изменить метод 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() 

изменить: добавляется аргумент буфера.

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() 
person Changju.rhee    schedule 25.09.2013