当前位置: 动力学知识库 > 问答 > 编程问答 >

How to make remotely file xCopy/delete batch program with Python

问题描述:

I'm beginner Python. I have many PCs connected with network switch. One is manager PC. others are client PCs. I'm going to make remotely file cCopy/delete batch program such as DOS batch command in Python.

any help how to start ?

DOS Batch command

xcopy E:\Share_main\A*.* \124.122.11.101\A\ /e /h /k /Y

网友答案:

Here is sample code.

#!/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))
分享给朋友:
您可能感兴趣的文章:
随机阅读: