場合によっては、大きなファイルを他の人に送信する必要がありますが、電子メールの添付ファイルのサイズ制限やネットワークの状態があまり良くないなど、送信チャネルの制限により、ファイルを分割する必要があります。大きなファイルを小さなファイルに分割して複数回送信すると、受信側でこれらの小さなファイルがマージされます。今回はPythonを使って大きなファイルを分割・結合する方法を紹介します。
アイデアと実装
テキストファイルであれば行数で区切ることができます。テキストファイルでもバイナリファイルでも、指定したサイズに応じて分割できます。
Python のファイル読み取りおよび書き込み機能を使用すると、ファイルを分割および結合し、各ファイルのサイズを設定し、指定したサイズのバイトを読み取り、新しいファイルに書き込むことができます。受信側は小さいファイルを読み取ります。ファイルに読み込んだバイトを順番にファイルに書き込むと、マージが完了します。
Split
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)
Merge
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
Split
from filesplit.split import Split split = Split("./data.rar", "./output") split.bysize(size = 1024*1000*10) # 每个文件最多 10MB
実行後、出力フォルダーに分割されたファイルが表示されます:
ファイル内の行数に応じて分割することもできます:
split.bylinecount(linecount = 10000) # 每个文件最多 10000 行
マージ
マージするには、フォルダー内に小さなファイルが必要です, このツールでは、フォルダー内にマニフェスト ファイルが存在する必要があります。その形式は次のとおりです:
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()
実行後、マージ ディレクトリにマージされたファイルが表示されます:
以上がPython チュートリアル: Python を使用して大きなファイルを分割および結合するにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

pythonusesahybridmodelofcompilation andtertation:1)thepythoninterpretercompilessourcodeodeplatform-indopent bytecode.2)thepythonvirtualmachine(pvm)thenexecuteTesthisbytecode、balancingeaseoputhswithporformance。

pythonisbothintersedand compiled.1)it'scompiledtobytecode forportabalityacrossplatforms.2)bytecodeisthenは解釈され、開発を許可します。

loopsareideal whenyouwhenyouknumberofiterationsinadvance、foreleloopsarebetterforsituationsは、loopsaremoreedilaConditionismetを使用します

henthenumber ofiterationsisknown advanceの場合、dopendonacondition.1)forloopsareideal foriterating over for -for -for -saredaverseversives likelistorarrays.2)whileopsaresupasiable forsaresutable forscenarioswheretheloopcontinupcontinuspificcond

pythonisnotpurelyLepted; itusesahybridapproachofbytecodecodecodecodecodecodedruntimerttation.1)pythoncompilessourcodeintobytecode、whodythepythonvirtualmachine(pvm).2)

ToconcatenateListsinpythothesheElements、使用:1)Operatortokeepduplicates、2)asettoremoveduplicates、or3)listcomplunting for controloverduplicates、各メトドハスディフェルフェルフェントパフォーマンスアンドソーダーインプリテーション。

pythonisantertedlanguage、useaseofuseandflexibility-butfactingporformantationationsincriticalapplications.1)解釈されたlikepythonexecuteline-by-lineを解釈します

Useforloopswhenthenumberofiterationsisknowninadvance、andwhiloopswheniterationsdependonacondition.1)forloopsareidealforsecenceslikelistoranges.2)


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

SublimeText3 Linux 新バージョン
SublimeText3 Linux 最新バージョン

ZendStudio 13.5.1 Mac
強力な PHP 統合開発環境

EditPlus 中国語クラック版
サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

SAP NetWeaver Server Adapter for Eclipse
Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

MantisBT
Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。
