https://cloud.tencent.com/developer/article/2187817
fsutil file createnew D:\txt_1m.txt 1048576
# !/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()
标签:TXT,指定,生成,flag,file,time,print,now,size
From: https://www.cnblogs.com/frankJ/p/18319847/_yxl_file_generator_txt