Last active
April 20, 2023 14:40
-
-
Save kujirahand/40f056ebc8918bb9eec01a703e479e85 to your computer and use it in GitHub Desktop.
ファイルの分割スクリプト(python3版)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, sys, re | |
# ファイルを分割する関数 --- (*1) | |
def split_file(file_path, chunk_size): | |
# ファイルを開く --- (*2) | |
with open(file_path, 'rb') as f: | |
fileno = 0 | |
chunk = f.read(chunk_size) # 分割サイズ分読み込む --- (*3) | |
while chunk: | |
# 出力ファイル名を決める --- (*4) | |
output_filename = f"{file_path}.parts_{fileno:02d}.bin" | |
print(f"[出力{fileno}] {output_filename}") | |
# 分割ファイルを出力 --- (*5) | |
with open(output_filename, 'wb') as output: | |
output.write(chunk) | |
fileno += 1 | |
chunk = f.read(chunk_size) | |
# Windows用にバッチファイルを生成する --- (*6) | |
basename = os.path.basename(file_path) | |
files = [f'{basename}.parts_{no:02d}.bin' for no in range(0, fileno)] | |
with open(file_path + '.bat', 'wt', encoding='shift_jis') as bat: | |
files_str = ' + '.join(files) | |
bat.write('@echo off\r\n') | |
bat.write(f'copy /b {files_str} out_{basename} > nul') | |
# macOS/Linux用にシェルスクリプトを生成する --- (*7) | |
with open(file_path + '.sh', 'wt', encoding='utf-8') as sh: | |
files_str = ' '.join(files) | |
sh.write('#!/bin/bash\n') | |
sh.write(f'cat {files_str} > out_{basename}') | |
# 3MBとか1GBとかの文字列をバイト数に変換する関数 --- (*8) | |
def get_chunk_size(chunk_size_str): | |
# 分割サイズを取得 | |
r = re.match(r'^(\d+)(b|kb|mb|gb)$', chunk_size_str) | |
if not r: | |
print("[エラー] 分割サイズは1GBのように指定してください"); quit() | |
chunk_size = int(r.group(1)) | |
unit = r.group(2) | |
if unit == 'kb': chunk_size = chunk_size * 1024 | |
elif unit == 'mb': chunk_size = chunk_size * 1024 * 1024 | |
elif unit == 'gb': chunk_size = chunk_size * 1024 * 1024 * 1024 | |
else: print('不正な単位です:', unit); quit() | |
return chunk_size | |
if __name__ == "__main__": | |
# コマンドライン引数を取得 --- (*9) | |
if len(sys.argv) != 3: | |
print("[使い方] python3 split_file.py <file_path> <chunk_size>") | |
quit() | |
input_file_path = sys.argv[1] | |
chunk_size_str = sys.argv[2].lower() | |
chunk_size = get_chunk_size(chunk_size_str) | |
split_file(input_file_path, chunk_size) # 分割実行 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment