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

4
  • have you tried the "For large files" from here: stackoverflow.com/a/13613375/3430986 ? Commented Oct 31, 2014 at 18:29
  • It only works for text files Commented Oct 31, 2014 at 18:31
  • possible duplicate of Python concatenate text files Commented Oct 31, 2014 at 18:31
  • 1
    It only works for text files <- open with 'rb' - the b flag is for binary files Commented Oct 31, 2014 at 18:32

1 Answer 1

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))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.