1

I'm trying to merge two binary files to third binary file in Python. My code:

input1 = input2 = ""

input1 = open('input1.bin').read()
input2 = open('input2.bin').read()

input1 += input2 

with open('Output.bin', 'w') as fp:
    fp.write(input1)

This code is not giving me any error but this is not generating the expected output.

Like, if I wrote the batch command to merge the files:

copy /b input1.bin+input2.bin Output.bin

This command is generating the Output.bin of size 150KB whereas the earlier python command is giving me Output file size as 151KB.

I have tried this as well:

with open('Output.bin', 'wb') as fp:
    fp.write(input1)

i.e. to write using binary mode, but this gave me error as:

TypeError: a bytes-like object is required, not 'str'

What could be the correct process for this?


Referred this for earlier error: TypeError: a bytes-like object is required, not 'str' when writing to a file in Python3

This solution is not working.


Using Python 3.7

martineau
  • 119,623
  • 25
  • 170
  • 301
Bogota
  • 401
  • 4
  • 15
  • 2
    When reading with binary files, you should open them in `'b'` binary mode. i.e. `open('input1.bin', 'rb').read()` This will give you byte objects instead of strings. – martineau May 27 '20 at 18:39

2 Answers2

3

The reason you got this error ( TypeError: a bytes-like object is required, not 'str' when writing to a file in Python3) was because:
You read the file in text mode (which is the default mode) and so input1 and input2 became strings and you tried to write them back in binary mode (you need input1 to be bytes-like object). One way to do this is to read the file itself in binary mode like shown below.

# Try reading the file in binary mode and writing it back in binary 
# mode. By default it reads files in text mode  
input1 = open('input1.bin', 'rb').read()
input2 = open('input2.bin', 'rb').read()

input1 += input2 

with open('Output.bin', 'wb') as fp:
    fp.write(input1)
the23Effect
  • 580
  • 2
  • 7
0

I believe this should open both input files, read them in chunks, and write to one output file:

from shutil import copyfileobj
from io import DEFAULT_BUFFER_SIZE

with open('input1.bin', 'rb') as input1, open('input2.bin', 'rb') as input2, open('output.bin', 'wb') as output:
    copyfileobj(input1, output, DEFAULT_BUFFER_SIZE)
    copyfileobj(input2, output, DEFAULT_BUFFER_SIZE)

Terry Spotts
  • 3,527
  • 1
  • 8
  • 21