Nujud Ali
Nujud Ali

Reputation: 135

Split text file into smaller ones by bytes in python

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

Answers (1)

Andreas
Andreas

Reputation: 2521

If you are looking into splitting it into files of uniform size (e.g. 5KB each), then one solution would be:

  1. Read the large file as binary
  2. For every 5000 bytes (5KB), create a new file
  3. Write those 5000 bytes into the new file

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

Related Questions