在开发过程中会遇到有些大文件,因为比较大,无法打开的情况,这个时候需要进行文件分割,这里使用python进行文件分割,代码如下:
import os import math import sys block_size = int(sys.argv[2])*1024*1024 def split_file(filepath:str, block_size:int): filesize = os.path.getsize(filepath) blocks = math.ceil(filesize / block_size) last_block_size = block_size if filesize % block_size == 0 else filesize % block_size with open(filepath, 'rb') as fl: for i in range(blocks): filename = f'{filepath}.part{i}' with open(filename, 'wb') as fw: if i != blocks - 1: fw.write(fl.read(block_size)) else: fw.write(fl.read(last_block_size)) split_file(sys.argv[1], block_size)
使用方法为:python split.py txt 100
其中 split.py 为上述代码的python文件,txt为要分割的文件完整路径+文件名,100为要分割的大小,单位为MB
标签:分割,filepath,python,filesize,split,文本文件,block,size From: https://www.cnblogs.com/newthread/p/17616242.html