Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 如何用Python制作远程文件xCopy/delete批处理程序_Batch File_Python 3.x_Remote Access - Fatal编程技术网

Batch file 如何用Python制作远程文件xCopy/delete批处理程序

Batch file 如何用Python制作远程文件xCopy/delete批处理程序,batch-file,python-3.x,remote-access,Batch File,Python 3.x,Remote Access,我是Python初学者。我有许多电脑与网络交换机相连。一个是manager PC,另一个是客户端PC。我将制作远程文件复制/删除批处理程序,如Python中的DOS批处理命令。 如何开始有什么帮助吗 DOS批处理命令 xcopy E:\Share_main\A*.* \124.122.11.101\A\ /e /h /k /Y 下面是示例代码 #!/usr/bin/env python-3.x # -*- coding: utf-8 -*- # based on Carnival http:/

我是Python初学者。我有许多电脑与网络交换机相连。一个是manager PC,另一个是客户端PC。我将制作远程文件复制/删除批处理程序,如Python中的DOS批处理命令。 如何开始有什么帮助吗

DOS批处理命令

xcopy E:\Share_main\A*.* \124.122.11.101\A\ /e /h /k /Y
下面是示例代码

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

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


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

def multi_distribute_from_buffers(server, dirpath, filenames, subdir, buffers):

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

    index = 0
    for filename in filenames:
        remotepathfile = "%s/%s" % (remotepath, filename)        

        with open(remotepathfile, 'wb') as outFile:
            outFile.write(buffers[index]) 
            index = index + 1

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):
            print ("{}, Make dir {}".format(datetime.datetime.now(), d)) 
            os.makedirs(d)                    

        if not os.path.exists(path):
            print ("{}, Make dir {}".format(datetime.datetime.now(), path)) 
            os.makedirs(path)


def dist_mems(server_list, subdir):
    filecount = 0
    for dirpath, dirnames, filenames in os.walk(subdir):           
        make_dir(server_list, subdir, dirpath) 

        buffers = []
        for filename in filenames:
            pathname = os.path.join(dirpath, filename)    
            print("{}, {} : Read from {}".format(++filecount, datetime.datetime.now(), pathname)) 

            with open(pathname, 'rb') as inFile:
                buffers.append(inFile.read())

        for server in server_list:
            Process(target = multi_distribute_from_buffers,args=(server, dirpath, filenames, subdir, buffers)).start()


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

    return mydictionary

if __name__ == '__main__':  
    start = datetime.datetime.now()   

    clientListfile = 'C:\\Users\\Public\\client_list.csv'
    if(sys.argv[1] != ''): 
        clientListfile = sys.argv[1]

    sourceFolder = 'C:\\Users\\Public\\'
    if(sys.argv[2] != ''): 
        sourceFolder = sys.argv[2]

    server_list = get_server_list(clientListfile)
    dist_mems(server_list, sourceFolder)

    end = datetime.datetime.now()
    diff = end - start

    print(" XCopying Start to {} clients : {}".format(len(server_list), start))
    print(" XCopying finished : {}".format(end))
    print(" XCopying Total time span : {}".format(diff))