有時候,我們需要把一個大文件發送給別人,但是限於傳輸通道的限制,比如郵箱附件大小的限制,或者網絡狀況不太好,需要將大文件分割成小文件,分多次發送,接收端再對這些小文件進行合併。今天就來分享一下用 Python 分割合併大檔案的方法。
想法及實現
如果是文字文件,可以依行數分割。無論是文字文件還是二進位文件,都可以依指定大小進行分割。
使用Python 的文件讀寫功能就可以實現文件的分割與合併,設定每個文件的大小,然後讀取指定大小的字節就寫入一個新文件,接收端依次讀取小文件,把讀取到的位元組依序寫入一個文件,就可以完成合併。
分割
size = 1024 * 1000 * 10# 10MB with open("bigfile", "rb") as reader: part = 1 while True: part_content = reader.read(size) if not part_content: print("split done.") break with open(f"bigfile_part{part}","wb") as writer: writer.write(part_content)
#合併
total_parts = 5 with open("bigfile","wb") as writer: for i in range(5): with open(f"bigfile_part{i}", "rb") as reader: writer.write(reader.read())
使用第三方函式庫
雖然可以自己寫,但是別人寫好了,為什麼不省點時間直接用呢?直接pip 安裝就可以了:
pip install filesplit
分割
from filesplit.split import Split split = Split("./data.rar", "./output") split.bysize(size = 1024*1000*10) # 每个文件最多 10MB
執行之後,我們就可以在output 資料夾裡看到分割好的檔案:
你也可以依照檔案行數分割:
split.bylinecount(linecount = 10000) # 每个文件最多 10000 行
#合併
合併需要對資料夾裡的小文件進行合併,工具要求資料夾內必須有manifest 文件,其格式如下:
filename,filesize,header data_1.rar,10000000,False data_2.rar,10000000,False data_3.rar,10000000,False data_4.rar,10000000,False data_5.rar,1304145,False
合併文件的程式碼只需要指定要合併的目錄,目標目錄,合併後的檔案名,程式碼如下:
from filesplit.merge import Merge merge = Merge(inputdir = "./output", outputdir="./merge", outputfilename = "merged.rar") merge.merge()
執行之後就可以在merge 目錄內看到合併後的檔案:
以上是Python 教學:如何使用 Python 分割和合併大檔案?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

pythonisehybridmodeLofCompilation和interpretation:1)thepythoninterpretercompilesourcecececodeintoplatform- interpententbybytecode.2)thepythonvirtualmachine(pvm)thenexecutecutestestestestestesthisbytecode,ballancingEaseofuseEfuseWithPerformance。

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允許fordingfordforderynamictynamictymictymictymictyandrapiddefupment,儘管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

在您的知識之際,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations則youneedtoloopuntilaconditionismet

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

pythonisnotpuroly interpred; itosisehybridablectofbytecodecompilationandruntimeinterpretation.1)PythonCompiLessourceceCeceDintobyTecode,whitsthenexecececected bytybytybythepythepythepythonvirtirtualmachine(pvm).2)

concatenateListSinpythonWithTheSamelements,使用:1)operatoTotakeEpduplicates,2)asettoremavelemavphicates,or3)listcompreanspherensionforcontroloverduplicates,每個methodhasdhasdifferentperferentperferentperforentperforentperforentperfornceandordorimplications。

pythonisanterpretedlanguage,offeringosofuseandflexibilitybutfacingperformancelanceLimitationsInCricapplications.1)drightingedlanguageslikeLikeLikeLikeLikeLikeLikeLikeThonexecuteline-by-line,允許ImmediaMediaMediaMediaMediaMediateFeedBackAndBackAndRapidPrototypiD.2)compiledLanguagesLanguagesLagagesLikagesLikec/c thresst

Useforloopswhenthenumberofiterationsisknowninadvance,andwhileloopswheniterationsdependonacondition.1)Forloopsareidealforsequenceslikelistsorranges.2)Whileloopssuitscenarioswheretheloopcontinuesuntilaspecificconditionismet,usefulforuserinputsoralgorit


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能