Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #67762
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'encoding': 0.05; 'binary': 0.07; 'subject:file': 0.07; 'string': 0.09; 'bytes.': 0.09; 'encode': 0.09; 'image,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'skip:/ 10': 0.09; 'subject:string': 0.09; 'works.': 0.09; 'python': 0.11; '2.7': 0.14; 'mostly': 0.14; '""")': 0.16; '2.7.3': 0.16; 'choose,': 0.16; 'encodings': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'file,': 0.19; '>>>': 0.22; 'header :User-Agent:1': 0.23; 'replace': 0.24; 'skip:i 40': 0.24; 'file.': 0.24; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; 'character': 0.29; 'sep': 0.31; 'such.': 0.31; 'file': 0.32; 'another': 0.32; 'possible.': 0.35; 'convert': 0.35; 'clear': 0.37; 'whatever': 0.38; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'how': 0.40; 'read': 0.60; 'easy': 0.60; 'skip:o 30': 0.61; "you'll": 0.62; 'more': 0.64; 'mar': 0.68; 'otten': 0.84; 'received:pacbell.net': 0.84; 'subject:find': 0.84; '2013,': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | emile <emile@fenx.com> |
| Subject | Re: find and replace string in binary file |
| Date | Tue, 04 Mar 2014 16:13:00 -0800 |
| References | <01951a7d-2ab3-4203-a9c5-2f79017a980d@googlegroups.com> <lf4jrk$95v$1@ger.gmane.org> <CAPTjJmrZPr_QdejVwntYn17LSbuEp_8BsCmR5vfNU9N0PO4QYQ@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Gmane-NNTP-Posting-Host | adsl-69-226-129-65.dsl.pltn13.pacbell.net |
| User-Agent | Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 |
| In-Reply-To | <CAPTjJmrZPr_QdejVwntYn17LSbuEp_8BsCmR5vfNU9N0PO4QYQ@mail.gmail.com> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.7773.1393978398.18130.python-list@python.org> (permalink) |
| Lines | 45 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1393978398 news.xs4all.nl 2874 [2001:888:2000:d::a6]:34401 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:67762 |
Show key headers only | 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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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