当前位置: 首页 > news >正文

Python生成指定大小文件:txt/图片/视频/csv

如题,做测试的懂的都懂,不多解释

相比其他大佬,本脚本基于gpt编写后整理,生成的文件更真实,能够打开预览,看过其他人的生成脚本,只是一个符合大小,但是是空白或不能打开的文件。

话不多说,看示例,记得在创建一个data目录。

代码示例:

import os
import time
import csv
from PIL import Image
import random
import numpy as np
import imageio
import cv2# pip install opencv-python
# pip install Pillow
def generate_txt(file_size):file_size_bytes = 1024 * 1024 * file_sizefile_path = './data/txt' + time.strftime('%Y%m%d') + '_' + str(file_size) + 'M.txt'text = "Women only affect the speed at which I type Pythong code."  # 要重复的文本text_size_bytes = len(text.encode('utf-8'))  # 每个重复的文本的大小(以字节为单位)repetitions = file_size_bytes // text_size_bytes  # 需要重复的次数remainder = file_size_bytes % text_size_bytes  # 剩余的字节数with open(file_path, 'w') as file:for _ in range(repetitions):file.write(text)if remainder > 0:file.write(text[:remainder])print("生成完成")def generate_video(target_filesize_mb,  frame_width=1920, frame_height=1080, frame_rate=30):temp_filename = './data/image' + time.strftime('%Y%m%d') + '_' + str(target_filesize_mb) + 'M.mp4'fourcc = cv2.VideoWriter_fourcc(*"mp4v")out = cv2.VideoWriter(temp_filename, fourcc, frame_rate, (frame_width, frame_height))while True:frame = np.random.randint(0, 256, (frame_height, frame_width, 3), dtype=np.uint8)out.write(frame)current_filesize = (len(open(temp_filename, "rb").read())) / (1024 * 1024)  # in MBif current_filesize >= target_filesize_mb:breakout.release()def generate_image(memory_size, filename):""":param memory_size: 生成图片的大小,单位是m:param filename: 生成图片的文件格式:return:"""filename = './data/image'+ time.strftime('%Y%m%d') +'_'+ str(memory_size) + 'M' '.'+filename# 计算所需的像素数量num_pixels = (memory_size * 1024 * 1024) // 3  # 每个像素占用 3 个字节(RGB模式)# 根据像素数量计算图片的长和宽img_width = int(np.sqrt(num_pixels))img_height = int(num_pixels / img_width)# 创建一个随机颜色的数组pixels = np.random.randint(0, 256, (img_height, img_width, 3), dtype=np.uint8)# 根据数组创建图片对象image = Image.fromarray(pixels, 'RGB')image.save(filename)def generate_csv(target_memory_mb):file_name = './data/csv_utf8 ' + time.strftime('%Y%m%d') +"_"+ str(target_memory_mb) + 'M.csv'row_data = "Data" * 100  # Adjust length to control memory usage per rowwith open(file_name, 'w', newline='', encoding='utf-8') as csv_file:writer = csv.writer(csv_file)while os.path.getsize(file_name) / (1024 * 1024) < target_memory_mb:writer.writerow([row_data])if __name__ == '__main__':# 生成一个大小为2MB的TXT文件generate_txt(2)# 生成15M视频generate_video(target_filesize_mb=15)# 生成一个10M 的png的图片generate_image(10, "png")# 以utf-8的格式,生成一个10M的csv文件  CSV 文件的大小通常由数据量和内容决定,而不是像 Excel 那样可以直接控制行高和列宽。CSV 文件的大小可能会受到编码和分隔符的影响generate_csv(target_memory_mb=10)  # Change target memory size as needed
http://www.lryc.cn/news/125166.html

相关文章:

  • Arcgis中影像图切片有白斑或者白点
  • nlohmann json:通过[ ]运算符读取设置object/array
  • rust学习-tokio::time
  • Java 中 List 集合排序方法
  • prometheus监控k8s服务并告警到钉钉
  • Go和Java实现解释器模式
  • 域名配置HTTPS
  • 机械设计cad,ug编程设计,ug模具设计,SolidWorks模具设计
  • 嵌入式开发的学习与未来展望:借助STM32 HAL库开创创新之路
  • WPS-0DAY-20230809的分析和利用复现
  • MongoDB(三十九)
  • InnoDB引擎
  • CSS3中的var()函数
  • opencv图片换背景色
  • JAVA语言:什么是懒加载机制?
  • jupyter默认工作目录的更改
  • Flutter系列文章-Flutter UI进阶
  • Elasticsearch在部署时,对Linux的设置有哪些优化方法?
  • 【网络基础】应用层协议
  • 面试八股文Mysql:(1)事务实现的原理
  • Linux学习之sed多行模式
  • 【刷题笔记8.15】【链表相关】LeetCode:合并两个有序链表、反转链表
  • 神经网络基础-神经网络补充概念-11-向量化逻辑回归
  • openGauss学习笔记-40 openGauss 高级数据管理-锁
  • 勘探开发人工智能技术:机器学习(6)
  • 代理类型中的 HTTP、HTTPS 和 SOCKS 有什么区别?
  • 【STM32RT-Thread零基础入门】 3. PIN设备(GPIO)的使用
  • fiddler抓包工具的用法以及抓取手机报文定位bug
  • spring中时间格式化的两种方式
  • 【设计模式】原型模式