
To read the file, I have taken the already created file document.bin and used the “rb” mode to read the binary file.Sentence = bytearray("This is good".encode("ascii")) And then to close the file, I have used the file.close().Įxample to write the file: file = open("document.bin","wb") The write() is used to write the specified text to the file.And to write the sentence in the file, I have used the file.write() method.I have taken a variable as a sentence and assigned a sentence “This is good”, To decode the sentence, I have used sentence = bytearray(“This is good”.encode(“ascii”)).The document.bin is the name of the file.In this example, I have opened a file using file = open(“document.bin”,”wb”) and used the “wb” mode to write the binary file. Before reading a file we have to write the file.Here, we will see how to read a binary file in Python.
/do0bihdskp9dy.cloudfront.net/10-16-2022/t_f64a8df64fc34a2f816636a00b62ea2e_name_file_1280x720_2000_v3_1_.jpg)
