I need to write program that will change bytes in file in specific addreses. I can use only python 2.2 it's game's module so... I read once about mmap but i can't find it in python 2.2
Asked
Active
Viewed 201 times
1
-
1Is the file small enough to be read into memory at once? – Tim Pietzcker Oct 01 '12 at 15:25
-
You can use the code from this answer to a similar question: [How to overwrite some bytes in the middle of a file with Python?][1] [1]: http://stackoverflow.com/a/509014/1400944 – jcibar Nov 07 '13 at 16:53
1 Answers
1
Your best option is to manipulate the file directly; this will work regarding of Python version, i.e., 1.x, 2.x, 3.x. Here is some rough outline to get you started... if you do the actual pseudocode, it'll probably be pretty close if not exactly the correct Python:
- open the file for 'r+b' (read/write; for POSIX systems, you can also just use 'r+')
- go to the specific byte in question (use a file's
tell()
method) - write out the single byte you want changed (use a file's
write()
method) - close the file (use a file's
close()
method)

wescpy
- 10,689
- 3
- 54
- 53
-
Good point. In POSIX systems, it's not needed as they use binary mode by default, but if you're on a PC, the 'b' is needed as you describe. Edited... thanks! – wescpy Oct 01 '12 at 17:08
-
-
1why would you use `'r+'` that doesn't work on some platforms if you know that `r+b` works on *all* platforms including MacOS? In addition `b` communicates the programmer's intent to read bytes (data, not text) and it also works on Python 3. – jfs Oct 01 '12 at 17:47
-