How to save binary files in python?

0

I'm new to SOPt and would like to know how to save and open binary files in python.

If possible I would like to see examples of how this is done and what the parameters mean.

    
asked by anonymous 13.11.2018 / 21:00

1 answer

2

Every file is a sequence of bytes, so every file is "binary" . The thing that you can store in a file is bytes .

Whatever you are thinking of saving to a file, it will have to convert to bytes first.

Many times you want to store phrases, such as "Olá, sou um arquivo" , within files, but files only accept bytes ...

To solve this, we have created encodings that represent the letters in bytes:

 >>> frase = "Olá, sou um arquivo"
 >>> binario = frase.encode('utf32')
 >>> print([byte for byte in binario])
 [255, 254, 0, 0, 79, 0, 0, 0, 108, 0, 0, 0, 225, 0, 0, 0, 44, 0, 0, 0, 32, 0, 0, 0, 
  115, 0, 0, 0, 111, 0, 0, 0, 117, 0, 0, 0, 32, 0, 0, 0, 117, 0, 0, 0, 109, 0, 0, 0,
  32, 0, 0, 0, 97, 0, 0, 0, 114, 0, 0, 0, 113, 0, 0, 0, 117, 0, 0, 0, 105, 0, 0, 0,
  118, 0, 0, 0, 111, 0, 0, 0]

In this example the sentence was encoded using utf32 , generated 80 bytes , which can be written to a binary file:

 with open('arquivo.txt', 'wb') as f:  # 'w' para escrita e 'b' para modo binário
     f.write(binario)

The detail you often confuse is that python, starting with version 3, works by default with automatic encoding . If you open the file without putting the letter 'b' in the mode, it will encode and decode automatically for you any text that is written or read, converting from / to bytes transparently!

with open('arquivo.txt', 'w') as f:  # sem o 'b' abre em modo "texto"
    f.write(frase) # escreve a frase direto, o python resolve!

In this example it will use utf-8 to encode which is the default, but if you want you can use another encoding:

with open('arquivo.txt', 'w', encoding='utf32') as f:
    f.write(frase) # vai codificar automaticamente em utf32

Similarly, if you read the file binary, it will return bytes , but if you read in text mode, python will decode the bytes automatically and return you a string:

with open('arquivo.txt') as f:
    frase = f.read() # já retorna str
    print(frase)

In short: Every file is binary and stores bytes , what changes is how you treat it. If you open it in binary form, you will have to read and write bytes, if you open in text mode python takes care of the encoding and you can handle strings directly.

    
14.11.2018 / 01:54