python递归创建文件

  1. 功能目标

使用python脚本创建文件,内容为时间戳+随机字符,确保文件hash唯一;

可自定义【文件夹层级深度,每层级文件夹下子文件夹数目,每个子文件夹下文件数目,文件大小】;

生成文件在脚本同一目录下。

  1. 代码实现:

import sys,os
import time
import string
import random
import time
import datetime

def time_stamp_10():
    return str(int(time.time()))

def get_current_time():
    return datetime.datetime.now().strftime('%Y%m%d_%H%M%S_%f')

def create_dir(parentpath,cengji,subdircount,filecount,filesize):
    cengji -= 1
    for i in range(subdircount):
        subdirname = parentpath+'/'+'{cengji}层{i}号_{stamp}'.format(cengji=文件夹层级深度-cengji,i=i+1,stamp=get_current_time())
        os.mkdir(subdirname)
        create_file(subdirname,filecount,filesize,cengji)
        if cengji > 0:
            create_dir(subdirname,cengji,subdircount,filecount,filesize)

def create_file(dirpath,filecount,filesize,cengji):
    if filecount>0:
        for f in range(filecount):
            try:
                # 文件内容
                s = random.choice(string.digits) + random.choice(string.ascii_letters) + random.choice(
                    string.digits) + random.choice(string.ascii_letters) + random.choice(string.digits) + \
                    random.choice(string.ascii_letters) + random.choice(string.digits) + random.choice(
                    string.ascii_letters) + random.choice(string.digits) + random.choice(string.ascii_letters) + \
                    random.choice(string.digits) + random.choice(string.ascii_letters) + random.choice(
                    string.digits) + random.choice(string.ascii_letters) + random.choice(string.ascii_letters) + \
                    random.choice(string.digits) + random.choice(string.ascii_letters) + random.choice(
                    string.ascii_letters) + random.choice(string.digits) + random.choice(string.ascii_letters) + \
                    random.choice(string.ascii_letters) + random.choice(string.digits) + random.choice(
                    string.ascii_letters) + random.choice(string.ascii_letters) + time_stamp_10() * 100 + '\n'
                fn = '{dirpath}/{cengji}_{f}_{timestamp}.txt'.format(dirpath=dirpath, cengji=文件夹层级深度-cengji, f=str(f+1),timestamp=get_current_time())
                with open(fn, 'a+') as ff:
                    for i in range(filesize):
                        ff.write(s*1024) # M
                        # ff.write(s)     # K
                        # print('{fn}--> :{percent}%'.format(fn=fn, percent=str(int(i / filesize * 100))))
                    # print('{fn}--> :100%'.format(fn=fn))
                    print('成功新建文件:{fn}'.format(fn=fn))
            except Exception as e:
                print(e)


if __name__ == '__main__':
    '''快速生成测试文件'''
    '''
    某数字X从1次方加到N次方之和公式: 和=x(1-x^N)/(1-x)
    '''
    
    name_com = '3层夹_2子文件夹_1个4M文件' # 自动创建一个根目录的名字
    
    global 文件夹层级深度
    文件夹层级深度 = 3       # 根目录下子文件夹层级深度
    
    每层级文件夹数目 = 2    # 每层文件夹下子文件夹的个数
    每文件夹下文件数目 = 1   # 每个文件夹下创建文件的数目,如果只需要文件则为 0
    单个文件大小 = 4       # 每个文件大小,单位 M,必须int类型,最小为1

    rootdir = './{rootdirname}_{t}'.format(rootdirname=name_com,t=get_current_time())
    if not os.path.exists(rootdir):
        os.mkdir(rootdir)
    create_dir(rootdir, 文件夹层级深度,每层级文件夹数目, 每文件夹下文件数目,单个文件大小)
    print("   >>>>>>>>>>>>>>>>>>>>>  脚本结束 :{dirname} <<<<<<<<<<<<<<<<<<<<<<<   ".format(dirname=name_com))
  1. 参数说明

name_com = '3层夹_2子文件夹_1个4M文件' # 自动创建一个根目录的名字

文件夹层级深度 = 3 # 根目录下子文件夹层级深度

每层级文件夹数目 = 2 # 每层文件夹下子文件夹的个数

每文件夹下文件数目 = 1 # 每个文件夹下创建文件的数目,如果只需要文件夹则为 0

单个文件大小 = 4 # 每个文件大小,单位 M,必须int类型,最小为1

  1. 运行效果:

  1. 文件目录:

python递归创建文件_第1张图片

你可能感兴趣的:(python,python,递归,自动创建文件)