Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #67762

Re: find and replace string in binary file

From emile <emile@fenx.com>
Subject Re: find and replace string in binary file
Date 2014-03-04 16:13 -0800
References <01951a7d-2ab3-4203-a9c5-2f79017a980d@googlegroups.com> <lf4jrk$95v$1@ger.gmane.org> <CAPTjJmrZPr_QdejVwntYn17LSbuEp_8BsCmR5vfNU9N0PO4QYQ@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.7773.1393978398.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 03/04/2014 02:44 PM, Chris Angelico wrote:
> On Wed, Mar 5, 2014 at 12:18 AM, Peter Otten <__peter__@web.de> wrote:
>> loial wrote:
>>
>>> How do I read a binary file, find/identify a character string and replace
>>> it with another character string and write out to another file?
>>>
>>> Its the finding of the string in a binary file that I am not clear on.
>>
>> That's not possible. You have to convert either binary to string or string
>> to binary before you can replace. Whatever you choose, you have to know the
>> encoding of the file.
>
> If it's actually a binary file (as in, an executable, or an image, or
> something), then the *file* won't have an encoding, so you'll need to
> know the encoding of the particular string you want and encode your
> string to bytes.


On 2.7 it's as easy as it sounds without having to think much about 
encodings and such.  I find it mostly just works.

emile@paj39:~$ which python
/usr/bin/python
emile@paj39:~$ python
Python 2.7.3 (default, Sep 26 2013, 16:38:10)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> image = open('/usr/bin/python','rb').read()
 >>> image.find("""Type "help", "copyright", "credits" """)
1491592
 >>> image = image[:1491592]+"Echo"+image[1491592+4:]
 >>> open('/home/emile/pyecho','wb').write(image)
 >>>
emile@paj39:~$ chmod a+x /home/emile/pyecho
emile@paj39:~$ /home/emile/pyecho
Python 2.7.3 (default, Sep 26 2013, 16:38:10)
[GCC 4.7.2] on linux2
Echo "help", "copyright", "credits" or "license" for more information.

YMMV,

Emile

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

find and replace string in binary file loial <jldunn2000@gmail.com> - 2014-03-04 04:27 -0800
  Re: find and replace string in binary file MRAB <python@mrabarnett.plus.com> - 2014-03-04 13:08 +0000
  Re: find and replace string in binary file Peter Otten <__peter__@web.de> - 2014-03-04 14:18 +0100
  Re: find and replace string in binary file Chris Angelico <rosuav@gmail.com> - 2014-03-05 09:44 +1100
  Re: find and replace string in binary file emile <emile@fenx.com> - 2014-03-04 16:13 -0800
    Re: find and replace string in binary file loial <jldunn2000@gmail.com> - 2014-03-05 01:59 -0800
      Re: find and replace string in binary file Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-05 12:42 +0000
      Re: find and replace string in binary file Emile van Sebille <emile@fenx.com> - 2014-03-05 09:46 -0800
  Re:find and replace string in binary file Dave Angel <davea@davea.name> - 2014-03-05 07:06 -0500

csiph-web