码迷,mamicode.com
首页 > 编程语言 > 详细

python 多进程练习 调用 os.system命令

时间:2019-02-20 21:35:35      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:rac   filename   python   directory   white   tip   top   调用   spl   

 

 

import sys
import getopt
import os
import multiprocessing


def list_all_file(path):
    """
    list all files of a directory
    :param path:
    :return:
    """
    file_list = []
    for (path, _, files) in os.walk(path):
        for filename in files:
            file_list.append(os.path.join(path, filename))
    return file_list


def process_file(filename, is_black):
    suffix = os.path.splitext(filename)[-1][1:]
    if suffix != "pcap":
        return

    if is_black:
        cmd = "python extract_tls_flow4.py -vr {} -o black/{}.txt  >logs/black/{}.log".format(filename, os.path.basename(filename), os.path.basename(filename))
    else:
        cmd = "python extract_tls_flow4.py -vr {} -o white/{}.txt  >logs/white/{}.log".format(filename, os.path.basename(filename), os.path.basename(filename))
    os.system(cmd)


def process_black_file(filename):
    process_file(filename, 1)


def process_white_file(filename):
    process_file(filename, 0)


def process_dir(sample_dir, is_black):
    file_list = list_all_file(sample_dir)

    process_num = 30
    pool = multiprocessing.Pool(processes=process_num)
    if is_black:
        pool.map(process_black_file, file_list)
    else:
        pool.map(process_white_file, file_list)

    pool.close()
    pool.join()
    print("End...........")


black_sample_dir = "/opt/data/samples/black_pcap"
white_sample_dir = "/opt/data/samples/white_pcap"

process_dir(black_sample_dir, 1)
process_dir(white_sample_dir, 0)

  

python 多进程练习 调用 os.system命令

标签:rac   filename   python   directory   white   tip   top   调用   spl   

原文地址:https://www.cnblogs.com/bonelee/p/10409271.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!