首页 > 编程语言 > 详细

Python生成指定容量文本

时间:2020-06-23 11:34:30      阅读:63      评论:0      收藏:0      [点我收藏+]

前言

用到相关库:
      OS
      random

生成指定容量文本

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date     : 2020/06/23 10:59:55
# @File     : 生成指定大小文件.py
# @Link     : https://www.cnblogs.com/BenLam/

import os
import random

def _tmp():
    li = []
    for _ in range(random.randint(10, 80)):
        li.append(chr(random.randint(65, 122)))
    str_li = str(li).replace("‘, ‘", "").strip("[‘]")
    return str_li

def test():
    # 默认是 1MB 容量文本
    MB = 1024*1024
    GB = 1024*1024*1024
    ds , fileSize = 0, MB
    filePath = "随机生成容量.txt"
    with open(filePath, "w", encoding="utf8") as f:
        while ds < fileSize:
            f.write(_tmp())
            f.write("\n")
            ds = os.path.getsize(filePath)
if __name__ == ‘__main__‘:
    test()

Python生成指定容量文本

原文:https://www.cnblogs.com/BenLam/p/13181019.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!