import os
import random
import string
from concurrent.futures import ThreadPoolExecutor
def generate_random_filename(length=10):
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(length))
def write_random_to_file(file_path, size):
with open(file_path, 'wb') as file:
file.write(os.urandom(size))
def create_files(file_count, file_size):
os.makedirs('test', exist_ok=True)
params = [
('test/' + generate_random_filename(20) + '.txt', file_size)
for _ in range(file_count)
]
with ThreadPoolExecutor(max_workers=None) as executor:
list(executor.map(lambda param: write_random_to_file(*param), params))
if __name__ == "__main__":
# 生成文件数量
file_count = 2000
# 单个文件大小,单位字节 1024=1kb
file_size = 1024 * 1024
create_files(file_count, file_size)
python使用线程池ThreadPoolExecutor快速生成文件将硬盘写满
简介:
只需要1秒钟就能生成1000个1M大小并且内容不同的文件。
下一篇: 没有了
相关文章
- php开发微信APP支付接口
- Vue项目中eslint提示 Newline required at end of file but
- html5中获取localStorage全部数据
- Vue项目中eslint提示 'xxx' is defined but never used
- 使用docker快速搭建nginx+php环境
- mpvue开发小程序下拉刷新、上拉加载
- Win10自带linux子系统WSL使用教程
- Laravel报错'Whoops, looks like something went wrong.'解决办法
- npm安装模块时报错:no such file or directory
- vue异步赋值数据无法获取的解决方法