测试文件生成工具

Zss 发表于:

测试FTP文件同步时,需要来产生大量的文件,来测试文件同步的情况

#coding:gbk
import os,time,multiprocessing,re
from multiprocessing import Pool


def mkdir_file(file_path,config):
    data = config[4].strip('<--->')
    number = int(config[2])
    for i in range(1,number+1):
        file_path1 = file_path + '\\test%s%s'%(i,config[3])
        with open(file_path1,'a+') as f:
            f.write(data)

def remove_all(path):
    if not os.path.exists(path):
        os.mkdir(path)
    for i in os.listdir(path):
        path_file = os.path.join(path, i)
        if os.path.isfile(path_file):
            os.remove(path_file)
        else:
            remove_all(path_file)
    for i in os.listdir(path):
        path_file = os.path.join(path, i)
        if os.path.exists(path_file):
            os.removedirs(path_file)
        else:
            remove_all(path_file)
    if not os.path.exists(path):
        os.mkdir(path)


def mkdir_folder(config):
    for i in range(1,int(config[1])+1):
        folder_name = config[0] + '\%s-test%s'%(time.strftime("%Y-%m-%d"),i)
        os.mkdir(folder_name)

def read_config():
    if not os.path.isfile('config.ini'):
        with open('config.ini','a') as f:
            f.write('最外层文件夹路径-->C:\\FTP\n')
            f.write('最外层文件夹中生成多少个文件夹-->100\n')
            f.write('每个文件夹多少个文件-->100\n')
            f.write('文件的后缀-->.txt\n')
            f.write('文件内容填写在箭头中<--->Test-Page<--->\n')
    with open('config.ini','r') as f:
        config = f.readlines()
    with open('config.ini','r') as f:
        data = f.read()
    str_text = re.findall('<--->(.*)<--->',data,flags=re.DOTALL)
    mkdir_config = (config[0].split('-->')[1].strip(),config[1].split('-->')[1].strip(),config[2].split('-->')[1].strip(),config[3].split('-->')[1].strip(),str_text[0])
    print '-'*40 +'\n 外层文件夹路径:%s\n 生成文件夹个数:%s\n 每个文件夹生成文件个数:%s\n 文件总数:%s \n 文件后缀:%s\n'.decode('gbk')%(mkdir_config[0],mkdir_config[1],mkdir_config[2],int(mkdir_config[1])*int(mkdir_config[2]),mkdir_config[3])+'-'*40
    return mkdir_config

if __name__ == '__main__':
    multiprocessing.freeze_support()
    config = read_config()
    print '-->> 正在清空%s的文件夹和文件'.decode('gbk')%config[0]
    remove_all(config[0])
    print '-->> 开始在%s中创建文件夹和文件'.decode('gbk')%config[0]
    s_time = time.time()
    mkdir_folder(config)
    P = Pool()
    for i in os.listdir(config[0]):
        file_path = config[0] +'\%s'%i
        P.apply_async(mkdir_file,(file_path,config))
    P.close()
    P.join()
    e_time = time.time()
    print '-->> 创建完成!创建时长:%d秒'.decode('gbk')%(e_time-s_time)
    print '-' * 40
    input()