0

我是Python初学者。我有许多与网络交换机连接的 PC。一种是经理PC。其他是客户端 PC。我将在 Python 中制作远程文件 cCopy/delete 批处理程序,例如 DOS 批处理命令。任何帮助如何开始?

DOS批处理命令

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

1 回答 1

0

这是示例代码。

#!/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))
于 2013-10-08T02:27:19.277 回答