I want to read bytes from a file and then write those bytes to another file, and save that file.
How do I do this?
First, open a file in binary write mode and then specify the contents to write in the form of bytes. Next, use the write function to write the byte contents to a binary file.
Python Read Binary File into Byte Array First, the file is opened in the“ rb “ mode. A byte array called mybytearray is initialized using the bytearray() method. Then the file is read one byte at a time using f. read(1) and appended to the byte array using += operator.
Definition and Usage. The read() method returns the specified number of bytes from the file. Default is -1 which means the whole file.
Here's how to do it with the basic file operations in Python. This opens one file, reads the data into memory, then opens the second file and writes it out.
in_file = open("in-file", "rb") # opening for [r]eading as [b]inary data = in_file.read() # if you only wanted to read 512 bytes, do .read(512) in_file.close() out_file = open("out-file", "wb") # open for [w]riting as [b]inary out_file.write(data) out_file.close()
We can do this more succinctly by using the with
keyboard to handle closing the file.
with open("in-file", "rb") as in_file, open("out-file", "wb") as out_file: out_file.write(in_file.read())
If you don't want to store the entire file in memory, you can transfer it in pieces.
piece_size = 4096 # 4 KiB with open("in-file", "rb") as in_file, open("out-file", "wb") as out_file: while True: piece = in_file.read(piece_size) if piece == "": break # end of file out_file.write(piece)
In my examples I use the 'b' flag ('wb', 'rb') when opening the files because you said you wanted to read bytes. The 'b' flag tells Python not to interpret end-of-line characters which can differ between operating systems. If you are reading text, then omit the 'b' and use 'w' and 'r' respectively.
This reads the entire file in one chunk using the "simplest" Python code. The problem with this approach is that you could run out memory when reading a large file:
ifile = open(input_filename,'rb') ofile = open(output_filename, 'wb') ofile.write(ifile.read()) ofile.close() ifile.close()
This example is refined to read 1MB chunks to ensure it works for files of any size without running out of memory:
ifile = open(input_filename,'rb') ofile = open(output_filename, 'wb') data = ifile.read(1024*1024) while data: ofile.write(data) data = ifile.read(1024*1024) ofile.close() ifile.close()
This example is the same as above but leverages using with to create a context. The advantage of this approach is that the file is automatically closed when exiting the context:
with open(input_filename,'rb') as ifile: with open(output_filename, 'wb') as ofile: data = ifile.read(1024*1024) while data: ofile.write(data) data = ifile.read(1024*1024)
See the following:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With