2013-09-23 234 views
2

我是Python初學者。我正在使用多處理功能將來自主機PC的xcopy等程序批處理到遠程PC。我收到了許可錯誤,如* DUPLICATE_SAME_ACCESS *。任何幫助都會非常有幫助。PermissionError:[WinError 5]訪問被拒絕

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,測試

回答

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()