13

THIS SHOULD BE EASY! But i've been unable to find the answer to this question.

Using python, I want to read a binary file into memory, modify the first four bytes of the file, and then write the file back.

There has to be a simple way to edit four measly bytes! right?

2
  • Do you want to read the whole file into memory or only the first four bytes that you want to modify later? Commented Jan 5, 2011 at 0:35
  • 1
    So what have you actually tried? Show your best attempt, and say what went wrong. Commented Jan 5, 2011 at 0:36

6 Answers 6

18

Why read the entire file to change four bytes at the beginning? Shouldn't this work?

with open("filename.txt", "r+b") as f:
     f.write(chr(10) + chr(20) + chr(30) + chr(40))

Even if you need to read these bytes from the file to calculate the new values, you could still do:

with open("filename.txt", "r+b") as f:
    fourbytes = [ord(b) for b in f.read(4)]
    fourbytes[0] = fourbytes[1]  # whatever, manipulate your bytes here
    f.seek(0)
    f.write("".join(chr(b) for b in fourbytes))
Sign up to request clarification or add additional context in comments.

3 Comments

maybe the values of the new first 4 bytes depend on the rest of the file content?
Maybe this is a novice question, but how can you write to the file if you've opened it in "r+b" mode, which in my understanding stands for 'read' and 'binary'?
r means "read", + means "plus write," b means "binary". (basically "+" enables both reading and writing regardless of whether you specified "r", "w", or "a" mode.) the main difference then becomes whether the file is created if necessary ("w") and where the writes occur ("a" always writes at the end)
6
with open(filename, 'r+b') as f:
  bytes = f.read(4)
  newbytes = 'demo'
  f.seek(0)
  f.write(newbytes)

2 Comments

I like this more than mine, and it's simpler than mmap. Nice.
My issue was to use 'w+b' instead of 'r+b' as your solution. Thank you.
2
C:\junk>copy con qwerty.txt
qwertyuiop
^Z
        1 file(s) copied.

C:\junk>\python27\python
Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> f = open('qwerty.txt', 'r+b')
>>> f.write('asdf')
>>> f.close()
>>> open('qwerty.txt', 'rb').read()
'asdftyuiop\r\n'
>>>

Comments

1

Simply, but memory inefficiently,

The Python 3 way:

def binaryedit(fn):
 f=open(fn,mode='rb')
 fc=f.read()
 f.close()
 return b'rawr'+fc[4:]

The Python 2 way:

def binaryedit(fn):
 f=open(fn,mode='rb')
 fc=f.read()
 f.close()
 return 'rawr'+fc[4:]

If the files are huge, you can memory map them and edit/write just the bytes that need to change. There's barely any difference until they get over a meg or so, though.

3 Comments

"...modify the first four bytes of the file, and then write the file back."
Why not use f.read(4) to take the first 4 bytes, then f.read() at the end for the rest of it.
Thomas, I thought about that after I posted, good catch. I copy/pasted from one of my files into this and just changed the name and the last line. :p
0

this should help. http://www.johnny-lin.com/cdat_tips/tips_fileio/bin_array.html

import Numeric as N import array

num_lon = 144 num_lat = 73 tmpfile = "tmp.bin"

fileobj = open(tmpfile, mode='rb') binvalues = array.array('f') binvalues.read(fileobj, num_lon * num_lat)

data = N.array(binvalues, typecode=N.Float)

data = N.reshape(data, (num_lat, num_lon))

fileobj.close()

Comments

0

This looks a lot like HW so I will not give exact code. but here is enough information

  1. You dont need to read the whole file into memory for changing the first 4 bytes
  2. Open the file in mode 'r+b'
  3. Use f.seek(0) to seek to the begining
  4. Write 4 bytes of data using f.write('ABCD')
  5. Close the file

3 Comments

Sorry, mode='a' is not portable, as the manual says: """'a' for appending (which on some Unix systems means that all writes append to the end of the file regardless of the current seek position)."""
and now you don't need to seek :)
yup but since I did not submit code, its okay to give a general idea? What do you think?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.