Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python, how to read bytes from file and save it? [closed]

Tags:

python

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?

like image 556
user850019 Avatar asked Jul 22 '11 08:07

user850019


People also ask

How do I save a byte in Python?

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.

How do I read bytes in Python?

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.

What does read () return in Python?

Definition and Usage. The read() method returns the specified number of bytes from the file. Default is -1 which means the whole file.


2 Answers

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) 
like image 135
Jeremy Avatar answered Sep 21 '22 18:09

Jeremy


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:

  • Python open(): http://docs.python.org/library/functions.html#open
  • Python read(): http://docs.python.org/library/stdtypes.html#file.read
  • Python write(): http://docs.python.org/library/stdtypes.html#file.write
  • Dive into Python File Object Tutorial: http://diveintopython.net/file_handling/file_objects.html
like image 42
Mark Evans Avatar answered Sep 18 '22 18:09

Mark Evans