




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第python讀取一個(gè)大于10G的txt文件的方法用python讀取一個(gè)大于10G的文件,自己電腦只有8G內(nèi)存,一運(yùn)行就報(bào)內(nèi)存溢出:MemoryError
python如何用open函數(shù)讀取大文件呢?
讀取大文件
首先可以自己先制作一個(gè)大于10G的txt文件
a='''
2025-02-0221:33:31,678[django.request:93][base:get_response][WARNING]-NotFound:/44/
2025-02-0221:33:31,679[django.server:124][basehttp:log_message][WARNING]-"HEAD44/HTTP/1.1"4041678
2025-02-0222:14:04,121[django.server:124][basehttp:log_message][INFO]-code400,messageBadrequestversion('HTTP')
2025-02-0222:14:04,122[django.server:124][basehttp:log_message][WARNING]-"GET../../mnt/custom/ProductDefinitionHTTP"400-
2025-02-0222:16:21,052[django.server:124][basehttp:log_message][INFO]-"GET/api/loginHTTP/1.1"3010
2025-02-0222:16:21,123[django.server:124][basehttp:log_message][INFO]-"GET/api/login/HTTP/1.1"2003876
2025-02-0222:16:21,192[django.server:124][basehttp:log_message][INFO]-"GET/static/assets/img/main_bg.pngHTTP/1.1"2002801
2025-02-0222:16:21,196[django.server:124][basehttp:log_message][INFO]-"GET/static/assets/iconfont/style.cssHTTP/1.1"2001638
2025-02-0222:16:21,229[django.server:124][basehttp:log_message][INFO]-"GET/static/assets/img/bg.jpgHTTP/1.1"200135990
2025-02-0222:16:21,307[django.server:124][basehttp:log_message][INFO]-"GET/static/assets/iconfont/fonts/icomoon.ttfu4m6fyHTTP/1.1"2006900
2025-02-0222:16:23,525[django.server:124][basehttp:log_message][INFO]-"POST/api/login/HTTP/1.1"3020
2025-02-0222:16:23,618[django.server:124][basehttp:log_message][INFO]-"GET/api/index/HTTP/1.1"20018447
2025-02-0222:16:23,709[django.server:124][basehttp:log_message][INFO]-"GET/static/assets/js/commons.jsHTTP/1.1"20013209
2025-02-0222:16:23,712[django.server:124][basehttp:log_message][INFO]-"GET/static/assets/css/admin.cssHTTP/1.1"20019660
2025-02-0222:16:23,712[django.server:124][basehttp:log_message][INFO]-"GET/static/assets/css/common.cssHTTP/1.1"2001004
2025-02-0222:16:23,714[django.server:124][basehttp:log_message][INFO]-"GET/static/assets/js/app.jsHTTP/1.1"20020844
2025-02-0222:16:26,509[django.server:124][basehttp:log_message][INFO]-"GET/api/report_list/1/HTTP/1.1"20014649
2025-02-0222:16:51,496[django.server:124][basehttp:log_message][INFO]-"GET/api/test_list/1/HTTP/1.1"20024874
2025-02-0222:16:51,721[django.server:124][basehttp:log_message][INFO]-"POST/api/add_case/HTTP/1.1"2000
2025-02-0222:16:59,707[django.server:124][basehttp:log_message][INFO]-"GET/api/test_list/1/HTTP/1.1"20024874
2025-02-0322:16:59,909[django.server:124][basehttp:log_message][INFO]-"POST/api/add_case/HTTP/1.1"2000
2025-02-0322:17:01,306[django.server:124][basehttp:log_message][INFO]-"GET/api/edit_case/1/HTTP/1.1"20036504
2025-02-0322:17:06,265[django.server:124][basehttp:log_message][INFO]-"GET/api/add_project/HTTP/1.1"20017737
2025-02-0322:17:07,825[django.server:124][basehttp:log_message][INFO]-"GET/api/project_list/1/HTTP/1.1"20029789
2025-02-0322:17:13,116[django.server:124][basehttp:log_message][INFO]-"GET/api/add_config/HTTP/1.1"20024816
2025-02-0322:17:19,671[django.server:124][basehttp:log_message][INFO]-"GET/api/config_list/1/HTTP/1.1"20019532
whileTrue:
withopen("xxx.log","a",encoding="utf-8")asfp:
fp.write(a)
循環(huán)寫(xiě)入到xxx.log文件,運(yùn)行3-5分鐘,pycharm打開(kāi)查看文件大小大于10G
于是我用open函數(shù)直接讀取
f=open("xxx.log",'r')
print(f.read())
f.close()
拋出內(nèi)存溢出異常:MemoryError
Traceback(mostrecentcalllast):
File"D:/2025kecheng06/demo/txt.py",line35,inmodule
print(f.read())
MemoryError
運(yùn)行的時(shí)候可以看下自己電腦的內(nèi)存已經(jīng)占了100%,cpu高達(dá)91%,不掛掉才怪了!
這種錯(cuò)誤的原因在于,read()方法執(zhí)行操作是一次性的都讀入內(nèi)存中,顯然文件大于內(nèi)存就會(huì)報(bào)錯(cuò)。
read()的幾種方法
1.read()方法可以帶參數(shù)n,n是每次讀取的大小長(zhǎng)度,也就是可以每次讀一部分,這樣就不會(huì)導(dǎo)致內(nèi)存溢出
f=open("xxx.log",'r')
print(f.read(2048))
f.close()
運(yùn)行結(jié)果
2025-10-2421:33:31,678[django.request:93][base:get_response][WARNING]-NotFound:/44/
2025-10-2421:33:31,679[django.server:124][basehttp:log_message][WARNING]-"HEAD44/HTTP/1.1"4041678
2025-10-2422:14:04,121[django.server:124][basehttp:log_message][INFO]-code400,messageBadrequestversion('HTTP')
2025-10-2422:14:04,122[django.server:124][basehttp:log_message][WARNING]-"GET../../mnt/custom/ProductDefinitionHTTP"400-
2025-10-2422:16:21,052[django.server:124][basehttp:log_message][INFO]-"GET/api/loginHTTP/1.1"3010
2025-10-2422:16:21,123[django.server:124][basehttp:log_message][INFO]-"GET/api/login/HTTP/1.1"2003876
2025-10-2422:16:21,192[django.server:124][basehttp:log_message][INFO]-"GET/static/assets/img/main_bg.pngHTTP/1.1"2002801
2025-10-2422:16:21,196[django.server:124][basehttp:log_message][INFO]-"GET/static/assets/iconfont/style.cssHTTP/1.1"2001638
2025-10-2422:16:21,229[django.server:124][basehttp:log_message][INFO]-"GET/static/assets/img/bg.jpgHTTP/1.1"200135990
2025-10-2422:16:21,307[django.server:124][basehttp:log_message][INFO]-"GET/static/assets/iconfont/fonts/icomoon.ttfu4m6fyHTTP/1.1"2006900
2025-10-2422:16:23,525[django.server:124][basehttp:log_message][INFO]-"POST/api/login/HTTP/1.1"3020
2025-10-2422:16:23,618[django.server:124][basehttp:log_message][INFO]-"GET/api/index/HTTP/1.1"20018447
2025-10-2422:16:23,709[django.server:124][basehttp:log_message][INFO]-"GET/static/assets/js/commons.jsHTTP/1.1"20013209
2025-10-2422:16:23,712[django.server:124][basehttp:log_message][INFO]-"GET/static/assets/css/admin.cssHTTP/1.1"20019660
2025-10-2422:16:23,712[django.server:124][basehttp:log_message][INFO]-"GET/static/assets/css/common.cssHTTP/1.1"2001004
2025-10-2422:16:23,714[django.server:124][basehttp:log_message][INFO]-"GET/static/assets/js/app.jsHTTP/1.1"20020844
2025-10-2422:16:26,509[django.server:124][basehttp:log_message][I
這樣就只讀取了2048個(gè)字符,全部讀取的話,循環(huán)讀就行
f=open("xxx.log",'r')
whileTrue:
block=f.read(2048)
print(block)
ifnotblock:
break
f.close()
2.readline():每次讀取一行,這個(gè)方法也不會(huì)報(bào)錯(cuò)
f=open("xxx.log",'r')
whileTrue:
line=f.readline()
print(line,end="")
ifnotline:
break
f.close()
3.readlines():讀取全部的行,生成一個(gè)list,通過(guò)list來(lái)對(duì)文件進(jìn)行處理,顯然這種方式依然會(huì)造成:MemoyError
真正Pythonic的方法
真正Pythonci的方法,使用with結(jié)構(gòu)打開(kāi)文件,fp是一個(gè)可迭代對(duì)象,可以用for遍歷讀取每行的文件內(nèi)容
withopen("xxx.log",'r')asfp:
forlineinfp:
print(line,end="")
yield生成器讀取大文件
前面一篇講yield生成器的時(shí)候提到讀取大文件,函數(shù)返回一個(gè)可迭代對(duì)象,用next()方法讀取文件內(nèi)容
defread_file(fpath):
BLOCK_SIZE=1024
withopen(fpath,'rb')asf:
whileTrue:
block=f.read(BLOCK_SIZE)
ifblock:
yieldblock
else:
return
if__name__=='__main__':
a=read_file("xxx.log")
print(a)#generatorobjec
print(next(a))#bytes類型
print(next(a).decode("utf-8"))#str
運(yùn)行結(jié)果
generatorobjectread_fileat0x00000226B3005258
b'\r\n2025-10-2421:33:31,678[django.request:93][base:get_response][WARNING]-NotFound:/44/\r\n2025-10-2421:33:31,679[django.server:124][basehttp:log_message][WARNING]-"HEAD44/HTTP/1.1"4041678\r\n2025-10-2422:14:04,121[django.server:124][basehttp:log_message][INFO]-code400,messageBadrequestversion(\'HTTP')\r\n2025-10-2422:14:04,122[django.server:124][basehttp:log_message][WARNING]-"GET../../mnt/custom/ProductDefinitionHTTP"400-\r\n2025-10-2422:16:21,052[django.server:124][basehttp:log_message][INFO]-"GET/api/loginHTTP/1.1"3010\r\n2025-10-2422:16:21,123[django.server:124][basehttp:log_message][INFO]-"GET/api/login/HTTP/1.1"2003876\r\n2025-10-2422:16:21,192[django.server:124][basehttp:log_message][INFO]-"GET/static/assets/img/main_bg.pngHTTP/1.1"2002801\r\n2025-10-2422:16:21,196[django.server:124][basehttp:log_message][INFO]-"GET/static/assets/iconfont/style.cssHTTP/1.1"2001638\r\n2025-10-2422:16:21,229[django.server:124]'
[basehttp:log_message][INFO]-"GET/static/assets/img/bg.jpgHTTP/1.1"200135990
2025-10-2422:16:21,307[django.server:124][basehttp:log_messag
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 鶴壁市一模高三數(shù)學(xué)試卷
- 湖北武漢小升初數(shù)學(xué)試卷
- 淮南高一數(shù)學(xué)試卷
- 云南省石林彝族自治縣民族中學(xué)2025年物理高一下期末學(xué)業(yè)水平測(cè)試試題含解析
- 中國(guó)硅酸鹽水泥行業(yè)發(fā)展監(jiān)測(cè)及投資戰(zhàn)略研究報(bào)告
- 氨壓力表閥行業(yè)深度研究分析報(bào)告(2024-2030版)
- 2025年中國(guó)套鍋行業(yè)發(fā)展?jié)摿Ψ治黾巴顿Y方向研究報(bào)告
- 2025年中國(guó)電液舵機(jī)行業(yè)發(fā)展前景預(yù)測(cè)及投資規(guī)劃建議報(bào)告
- 2024年金屬基超硬材料項(xiàng)目資金籌措計(jì)劃書(shū)代可行性研究報(bào)告
- 藁城區(qū)早婚管理辦法細(xì)則
- 2023年上海市長(zhǎng)寧區(qū)高三年級(jí)下冊(cè)二模英語(yǔ)試卷含詳解
- 肺功能進(jìn)修總結(jié)匯報(bào)
- GB/T 3428-2024架空導(dǎo)線用鍍鋅鋼線
- 客運(yùn)駕駛員汛期安全培訓(xùn)
- 【1例心肌梗塞患者的PCI術(shù)后護(hù)理探究7800字(論文)】
- 中國(guó)特色社會(huì)主義民族發(fā)展理論研究
- 干部基本信息審核認(rèn)定表
- 采購(gòu)管理中的創(chuàng)新與持續(xù)改進(jìn)
- GB/T 5465.2-2023電氣設(shè)備用圖形符號(hào)第2部分:圖形符號(hào)
- 湖南省永州冷水灘區(qū)2021-2022學(xué)年七年級(jí)下學(xué)期期末語(yǔ)文試題答案
- 市政工程質(zhì)量通病防治措施
評(píng)論
0/150
提交評(píng)論