1

I have a text input file with the following content:

input.feature

3   0   1   1   193:1 225:1
2   0   1   1   57:1 1005:1
4   0   1   1   467:1 7:1
5   0   1   1   619:1 259:1

I want to convert it to a binary buffer file. This is my attempt using Python:

in_file = open('input.feature', 'r', buffering=1000)
out_file = io.open('output.buffer', 'wb')

out_byte_array = bytearray(in_file.read())
out_file.write(out_byte_array)
out_file.close()

The output file I get is not a binary file, it's a text file with the same content as input file. What am I doing wrong? I also tried using BytesIO but I couldn't figure out how to write bytesIO content to a binary file. Thanks in advance.

Parth Bhoiwala
  • 1,282
  • 3
  • 19
  • 44
  • A binary file writer 'wb' writes the data into the file byte by byte. You will able to see the data in output file because the characters in it written byte by byte not in the form of bytes. – Er. Harsh Rathore Jul 23 '19 at 15:13
  • Possible duplicate of [Convert string to binary in python](https://stackoverflow.com/questions/18815820/convert-string-to-binary-in-python) – Corentin Limier Jul 23 '19 at 15:15
  • @CorentinLimier He wants to write the one file's text data into other file into the form of bytes using binary writer 'wb'. – Er. Harsh Rathore Jul 23 '19 at 15:20
  • All files are binary - text files consist of the bytes that encode the text for a particular encoding. Can you show an example of the output that you expect? Also, is this Python2? – snakecharmerb Jul 24 '19 at 19:16

0 Answers0