Python 大文件切割:高效处理海量数据
在 Python 中,可以使用以下方法对大文件进行切割:
- 打开文件并读取内容:
with open('large_file.txt', 'r') as f:
content = f.read()
- 将内容切割成指定大小的块:
chunk_size = 1024 # 每个块的大小
chunks = [content[i:i+chunk_size] for i in range(0, len(content), chunk_size)]
- 将每个块写入新文件:
for i, chunk in enumerate(chunks):
with open(f'large_file_{i}.txt', 'w') as f:
f.write(chunk)
这将把'large_file.txt'文件分成多个大小为 1024 的块,并将每个块写入一个新的文件,文件名为'large_file_0.txt','large_file_1.txt' 等等。
原文地址: https://www.cveoy.top/t/topic/nAEt 著作权归作者所有。请勿转载和采集!