坚持为自己每月写1篇笔记

Retreat Hell!
We Just Got Here.

Python生成指定容量文本文档

前言

用到相关库:
      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()
posted @ 2020-06-23 11:10  l||||||l  阅读(262)  评论(0编辑  收藏  举报