生成指定大小的TXT文件

https://www.zxyty.com/
https://cloud.tencent.com/developer/article/2187817
"测试样本文件HW" * 5 * 1024 | out-file 100k.txt -Encoding UTF8
使用下面的脚本生成1M大小的标准txt
@echo off 

for /l %%n in (1,1,300) do (

type 1M.txt >> 300M.doc

)
# 但是文件有问题
from faker import Faker


def create_fake_data_file(file_path, size_in_bytes):
    faker = Faker()
    with open(file_path, "w") as f:
        while f.tell() < size_in_bytes:
            f.write(faker.name() + '\n')


if __name__ == '__main__':
    create_fake_data_file("D:\\1m_xls.xlsx", 1024 * 1024)
# !/user/bin/env python
# -*- coding:utf-8 -*-

import time


# 获取时间和日期
def get_now_datetime(flag=0):
    """
    flag = 0为时间和日期             eg:2018-04-11 10:04:55
    flag = 1仅获取日期               eg:2018-04-11
    flag = 2仅获取时间               eg:10:04:55
    flag = 3纯数字的日期和时间         eg:20180411100455
    """
    now = time.localtime(time.time())
    if flag == 0:
        return time.strftime('%Y-%m-%d %H:%M:%S', now)
    if flag == 1:
        return time.strftime('%Y-%m-%d', now)
    if flag == 2:
        return time.strftime('%H:%M:%S', now)
    if flag == 3:
        return time.strftime('%Y%m%d%H%M%S', now)


# 生成指定大小的TXT档
def generate_txt_of_certain_size():
    file_size = 0
    # 判断输入是否有误
    while True:
        size = input('请输入你想生成的TXT文件大小(MB):')
        if not size.strip().isdigit():
            print('只能输入整数,请重新输入!')
            continue
        else:
            file_size = int(size)
            break
    if file_size == 200:
        print('正在生成TXT文件,请稍候... ...')
    # 生成指定大小的TXT档
    filename = get_now_datetime(3) + '_' + size + 'MB.txt'
    print(f'文件名:{filename}')
    # 设置文件保存的路径
    filepath = 'D:\\'
    f = open(filepath + filename, 'w')
    # 获取开始时间
    start_time = get_now_datetime()
    for i in range(file_size):
        if i == 100:
            if i % 100 == 0:
                print(f'已生成{i // 100 * 100}MB数据.')
        for j in range(1024):
            try:
                f.write('^Huawei中华有为$' * 64)
            except KeyboardInterrupt:
                print('\n异常中断:KeyboardInterrupt')
                f.close()
                exit(-1)
    f.close()
    print(f'文件已成生并保存在D盘, 文件大小:{file_size}MB.\n')
    print(f'保存路径: {filepath + filename}')
    print(f'结束时间:{get_now_datetime()}')


if __name__ == '__main__':
    generate_txt_of_certain_size()

posted @ 2024-07-23 23:25  FrankirJ  阅读(18)  评论(0编辑  收藏  举报