生成指定大小的文件,如:字典类型,json类型,整型等
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | # -*- coding:utf-8 -*- # 生成指定大小文件 import json import os import random import numpy as np # 生成指定大小的文件 def genSizeFile(filename, filesize): file_path = "Data" + filename + ".txt" ds = 0 with open (file_path, "w" ) as f: while ds < filesize: f.write( str ( round (random.uniform( - 1000 , 1000 ), 2 ))) f.write( "\n" ) ds = os.path.getsize(file_path) print (ds) # 生成指定数量 def genNfile(fileNum): numCount = fileNum numRange = 3 * numCount tmpList = random.sample( range (numRange), numCount) print (tmpList) i = 0 filePath = " " + str(numCount) + " .txt" with open (filePath, "w" ) as f: while i < numCount: f.write( str (tmpList[i])) f.write( "\n" ) i + = 1 ds = os.path.getsize(filePath) print (ds) # 查看文件记录数 def countLine(thefilepath): count = 0 with open (thefilepath, 'rb' ) as thefile: while True : buffer = thefile.read( 100 * 1024 * 1024 ) if not buffer : break count + = str ( buffer ).count( "\n" ) print (count) def genJsonFileSize(KB): dic = { "power_switch" : True , "color" : 2 , "message" : "mece" , "low_voltage" : 24.6 , "time" : "202205111006" } flag = 0 size = 0 dic_json = {} while size < KB * 1024 : for i in dic.keys(): dic.update({i + str (flag): dic[i]}) dic_json = json.dumps(dic) # 字典转json with open ( './data.json' , 'w' ) as f: f.write(dic_json) size = os.path.getsize( './data.json' ) flag + = 1 print (dic_json) size_KB1 = size / 1024 size_KB2 = size % 1024 size = str (size_KB1) + '.' + str (size_KB2) print ( 'dicsize:' , size) return dic # 此处 传入的dic是作为str类型传入的 def join_json_file(dic): json_content = json.dumps({ "type" : "update" , "version" : 0 , "state" : { "reported" : dic, }, "clientToken" : "client_token2" }) with open ( './json_data.json' , 'w' ) as f: f.write( str (json_content)) size = os.path.getsize( './json_data.json' ) size_KB1 = size / 1024 size_KB2 = size % 1024 size = str (size_KB1) + '.' + str (size_KB2) print ( 'jsonsize:' , size) if __name__ = = '__main__' : #dic = genJsonFileSize(10) # join_json_file(dic) with open ( './adjust.json' , 'r' ) as f: content = f.read() print ( 'content' , len (content)) size = os.path.getsize( './adjust.json' ) size_KB1 = size / 1024 size_KB2 = size % 1024 size = str (size_KB1) + '.' + str (size_KB2) print ( 'jsonsize:' , size) |
本文来自博客园,作者:ReluStarry,转载请注明原文链接:https://www.cnblogs.com/relustarry/p/16258694.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统