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: 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 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> 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: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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