Reputation: 135
I have a problem with splitting a large text file into smaller files by the size (in bytes) e.g. text file has 30kB, I want to split it into multiple files with 5kB each.
I search a lot but I found almost ways split the file by lines.
any suggestions?
Upvotes: 1
Views: 3593
Reputation: 2521
If you are looking into splitting it into files of uniform size (e.g. 5KB each), then one solution would be:
Sample code:
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
Upvotes: 3