首页 > 代码库 > python 分割文件、组合文件

python 分割文件、组合文件

import glob

big_file = open(index.sql, rb) 

bak_file = index_bak
i = 1

while True:
    chunk = big_file.read(200000)
    if not chunk:
        break
    
    file_write = open(bak_file + str(i) + .sql, wb)
    file_write.write(chunk)
    file_write.close()
    i+=1

big_file.close()

big_file = index2.sql
file_merge = open(big_file, wb) 
list_file = glob.glob("index_bak*")
list_file = sorted(list_file, key=lambda d:d[len(bak_file)+1])
print(list_file)
for spt_file in list_file:
    big_file = open(spt_file, rb)
    file_merge.write(big_file.read())
    big_file.close()
    
file_merge.close()

 

python 分割文件、组合文件