0

How can I merge a few big files into 1 using python 3? it should work like the bash command

cat * > outfile

but it should work on Linux, Windows and OS X.

If I use

outfile = open("outfile", "wb")
for file in os.listdir():
    outfile.write(file.read())

it uses too much RAM

anton-tsyganenko
  • 147
  • 1
  • 1
  • 8

1 Answers1

2

For large binary files, instead of reading lines, read chunks that are a multiple of the disk block size. Something like (untested)

BLOCKSIZE = 4096  # typical, I believe
BLOCKS = 1024  # somewhat arbitrary
chunk = BLOCKS * BLOCKSIZE
with open("outfile", "wb") as outfile:
    for fname in os.listdir():
        with open('fname', "rb") as infile
            outfile.write(infile.read(chunk))
Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52