我在将大型文本文件按大小(以字节为单位)拆分为较小的文件时遇到问题,例如,文本文件具有30kB,我想将其拆分为每个大小为5kB的多个文件。
我进行了很多搜索,但发现几乎可以按行分割文件。
有什么建议?
如果您打算将其拆分为大小一致的文件(例如,每个文件大小为5KB),那么一种解决方案是:
样例代码:
i = 0
with open("large-file", "r", encoding="utf8") as in_file:
bytes = in_file.read(5000) # read 5000 bytes
while bytes:
with open("out-file-" + str(i), 'w', encoding="utf8") as output:
output.write(bytes)
bytes = in_file.read(5000) # read another 5000 bytes
i += 1
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句