最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python如何生成指定文件大小

 更新時(shí)間:2025年06月04日 14:06:24   作者:lewis_0  
這篇文章主要介紹了python如何生成指定文件大小的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

python生成指定文件大小

方法一(速度最快)

def create_file_fast(file_path, size_mb):
    """快速生成指定大小的空文件(全0填充)"""
    size_bytes = size_mb * 1024 * 1024
    with open(file_path, 'wb') as f:
        f.seek(size_bytes - 1)  # 定位到文件末尾前一字節(jié)
        f.write(b'\0')          # 寫入單個(gè)空字節(jié)

# 示例:生成500MB文件
if __name__ == '__main__:
	create_file_fast('100mb_file.bin', 500)

方法二(中等速度)

import os
import random

def create_random_file(file_path, size_mb):
    """生成包含隨機(jī)內(nèi)容的文件"""
    size_bytes = size_mb * 1024 * 1024
    chunk_size = 1024 * 1024  # 每次寫入1MB
    
    with open(file_path, 'wb') as f:
        while size_bytes > 0:
            chunk = min(chunk_size, size_bytes)
            # 生成隨機(jī)字節(jié)數(shù)據(jù)(比os.urandom更快)
            data = bytes(random.getrandbits(8) for _ in range(chunk))
            f.write(data)
            size_bytes -= chunk

# 示例:生成500MB文件
if __name__ == '__main__:
	create_random_file('random_500mb.bin', 500)

方法三(生成可讀文本文件–較慢)

import string
import random

def create_text_file(file_path, size_mb, line_length=80):
    """生成包含隨機(jī)文本的文件(帶換行符)"""
    size_bytes = size_mb * 1024 * 1024
    chars = string.ascii_letters + string.digits + string.punctuation + ' '
    
    with open(file_path, 'w') as f:
        while size_bytes > 0:
            # 生成一行文本
            line = ''.join(random.choices(chars, k=line_length))
            line += '\n'  # 添加換行符
            
            f.write(line)
            size_bytes -= len(line.encode('utf-8'))  # 計(jì)算UTF-8編碼后的字節(jié)大小

# 示例:生成500MB文本文件
if __name__ == '__main__:
	create_text_file('10mb_text.txt', 500)

方法四(使用內(nèi)存映射高效生成–超大文件)

import mmap
import os

def create_large_file(file_path, size_gb):
    """高效生成超大文件(GB級(jí)別)"""
    size_bytes = size_gb * 1024 ** 3
    
    # 創(chuàng)建空文件并設(shè)置大小
    with open(file_path, 'wb') as f:
        f.truncate(size_bytes)
    
    # 使用內(nèi)存映射填充隨機(jī)數(shù)據(jù)
    with open(file_path, 'r+b') as f:
        mm = mmap.mmap(f.fileno(), 0)
        chunk_size = 1024 * 1024 * 100  # 每次處理100MB
        for offset in range(0, size_bytes, chunk_size):
            end = min(offset + chunk_size, size_bytes)
            mm[offset:end] = os.urandom(end - offset)
        mm.close()

# 示例:生成5GB文件
if __name__ == '__main__:
	create_large_file('5gb_file.dat', 5)

方法五(生成特定模式文件)

def create_pattern_file(file_path, size_mb, pattern=b'ABCD'):
    """生成重復(fù)模式的文件(用于測試)"""
    size_bytes = size_mb * 1024 * 1024
    pattern_len = len(pattern)
    
    with open(file_path, 'wb') as f:
        # 計(jì)算完整模式塊數(shù)量
        full_blocks = size_bytes // pattern_len
        # 剩余字節(jié)
        remainder = size_bytes % pattern_len
        
        # 寫入完整塊
        for _ in range(full_blocks):
            f.write(pattern)
        
        # 寫入剩余部分
        if remainder:
            f.write(pattern[:remainder])

# 示例:生成500MB的重復(fù)模式文件
if __name__ == '__main__:
	create_pattern_file('pattern_50mb.dat', 500, b'TESTPATTERN')

方法六

def run(file_size):
    #輸入的文件大小去除首尾空格后切割,如果列表中只有一個(gè)數(shù)字說明是整數(shù),否則就是小數(shù)
    file_path = "D:/dir/"
    file_size = file_size
    print('file_size', file_size)
    new_file_size = file_size.strip()
    file_size_list = new_file_size.split(".")
    start = time.time()
    if len(file_size_list) == 1:
        int_size_mb(file_size)
        print("文件大小{}MB,已存入地址{}".format(file_size, file_path))
    else:
        int_size_mb(file_size)
        float_size_mb(file_size)
        print("文件大小{}MB,已存入地址{}".format(file_size,file_path))
    end = time.time()
    timer = end - start
    speed = float(file_size) / timer
    # print(start, end, timer, speed)
    logger.info('寫入文件共耗時(shí):' + str(timer) + ',寫入速度為:' + str(speed))
def int_size_mb(file_size):
    file_path = "D:/dir/"
    file_name = 'test_file'
    new_file_size = file_size.strip()
    file_size_list = new_file_size.split(".")
    #整數(shù)部分用寫入文件w方式
    with open(file_path+file_name,"w") as file:
        #b-kb-mb文件大小轉(zhuǎn)化
        for i in range(int(file_size_list[0])):
            for j in range(1024):
                file.write("01"*512)
def float_size_mb(file_size):
    file_path = "D:/dir/"
    file_name = 'test_file'
    new_file_size = file_size.strip()
    file_size_list = new_file_size.split(".")
    #小數(shù)部分用追加寫入a方法
    file_size = file_size
    with open(file_path+file_name,"a") as file:
        #獲取小數(shù)(單位mb)
        float_size_mb=float(file_size)-int(file_size_list[0])
        for i in range(1024):
            file.write("1"*int(1024*float_size_mb))

# 示例:生成500MB的文件           
if __name__ == '__main__':
	run(500)
	

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • django 將model轉(zhuǎn)換為字典的方法示例

    django 將model轉(zhuǎn)換為字典的方法示例

    平常的開發(fā)過程中不免遇到需要把model轉(zhuǎn)成字典的需求,這篇文章主要介紹了Django model轉(zhuǎn)字典的方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2018-10-10
  • python 常見的排序算法實(shí)現(xiàn)匯總

    python 常見的排序算法實(shí)現(xiàn)匯總

    這篇文章主要介紹了python 常見的排序算法,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
    2020-08-08
  • python實(shí)現(xiàn)apahce網(wǎng)站日志分析示例

    python實(shí)現(xiàn)apahce網(wǎng)站日志分析示例

    這篇文章主要介紹了python實(shí)現(xiàn)apahce網(wǎng)站日志分析示例,需要的朋友可以參考下
    2014-04-04
  • pyqt環(huán)境搭建教程

    pyqt環(huán)境搭建教程

    pyqt是一個(gè)用于創(chuàng)建GUI應(yīng)用程序的跨平臺(tái)工具包,它將python與qt庫融為一體,本文給大家分享pyqt環(huán)境搭建教程,感興趣的朋友一起看看吧
    2023-11-11
  • python實(shí)現(xiàn)大文本文件分割

    python實(shí)現(xiàn)大文本文件分割

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)大文本文件分割,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • 如何利用python實(shí)現(xiàn)把視頻轉(zhuǎn)換成gif圖形

    如何利用python實(shí)現(xiàn)把視頻轉(zhuǎn)換成gif圖形

    將視頻轉(zhuǎn)換為 GIF 圖形的重要性不言而喻,在信息快速傳播和多種社交平臺(tái)廣泛應(yīng)用的背景下,GIF 動(dòng)畫不僅為個(gè)人用戶提供了一種輕松的表達(dá)方式,本文給大家介紹了如何利用python實(shí)現(xiàn)把視頻轉(zhuǎn)換成gif圖形,需要的朋友可以參考下
    2024-10-10
  • python中p-value的實(shí)現(xiàn)方式

    python中p-value的實(shí)現(xiàn)方式

    今天小編就為大家分享一篇python中p-value的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • pandas的to_datetime時(shí)間轉(zhuǎn)換使用及學(xué)習(xí)心得

    pandas的to_datetime時(shí)間轉(zhuǎn)換使用及學(xué)習(xí)心得

    這篇文章主要給大家介紹了關(guān)于pandas的to_datetime時(shí)間轉(zhuǎn)換使用及學(xué)習(xí)心得的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用pandas具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • windows10下安裝TensorFlow Object Detection API的步驟

    windows10下安裝TensorFlow Object Detection API的步驟

    這篇文章主要介紹了windows10下安裝TensorFlow Object Detection API的步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-06-06
  • 詳解如何從TensorFlow的mnist數(shù)據(jù)集導(dǎo)出手寫體數(shù)字圖片

    詳解如何從TensorFlow的mnist數(shù)據(jù)集導(dǎo)出手寫體數(shù)字圖片

    這篇文章主要介紹了詳解如何從TensorFlow的mnist數(shù)據(jù)集導(dǎo)出手寫體數(shù)字圖片,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08

最新評(píng)論

丁青县| 万安县| 潮安县| 贡嘎县| 莱州市| 西昌市| 米脂县| 尉犁县| 江城| 深州市| 麟游县| 布尔津县| 望江县| 安义县| 会东县| 岑巩县| 合水县| 堆龙德庆县| 南皮县| 武功县| 旌德县| 临高县| 额济纳旗| 岗巴县| 南汇区| 宿松县| 长垣县| 柯坪县| 克什克腾旗| 江津市| 秀山| 海门市| 凤城市| 神农架林区| 锦州市| 望都县| 托克逊县| 柳河县| 兰溪市| 衡水市| 调兵山市|