-1
I am 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.
-1
I am 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.
2
Every file is a sequence of bytes, so, every file is "binary". To only thing that you can store in a healthy file bytes
.
Whatever you’re thinking of saving to a file, go had which convert into bytes first.
Often one wants to store phrases such as "Olá, sou um arquivo"
, within files, however, files only accept bytes
...
To solve this, encodings were created that represent letters in the form of 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 phrase was encoded using utf32
, generated 80 bytes
, which can be written in a binary form file:
with open('arquivo.txt', 'wb') as f: # 'w' para escrita e 'b' para modo binário
f.write(binario)
The detail that usually confuses is that python, starting with version 3, works by default with automatic coding. If you open the file without putting the letter 'b'
in the way, it will automatically encoding and decoding 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 he 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 binarially, it will return bytes
, however, if you read in text mode, python will automatically decode the bytes 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 the way you treat him. If you open it in binary form, you will have to read and write bytes, if you open it in text mode python takes care of the encoding and you can handle strings directly.
Browser other questions tagged python
You are not signed in. Login or sign up in order to post.
Hello Alysson, I translated your question, since this site is in Portuguese. I also removed the [tag:script] and [tag:mysql] tags as they do not seem to be related to the question. If for some reason I changed what you meant, just edit the question with the corrections that you consider appropriate.
– fernandosavio
There is the way
b
to open a file in binary form;rb
for reading,wb
for writing.– Woss