By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,592 Members | 1,541 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,592 IT Pros & Developers. It's quick & easy.

How to merge two binary files into one?

P: n/a
I want to merge file A and file B into a new file C,
All of them are binary.
How to do that?
thanks a lot.
--
鹦鹉聪明绝顶、搞笑之极,是人类的好朋友。
直到有一天,我才发觉,我是鹦鹉。
我是*墙的鹦鹉。
Jul 18 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On 2005-04-05, could ildg <co*******@gmail.com> wrote:
I want to merge file A and file B into a new file C, All of
them are binary.


file('C','wb').write(file('A','rb').read()+file('B ','rb').read())

--
Grant Edwards grante Yow! ... or were you
at driving the PONTIAC that
visi.com HONKED at me in MIAMI last
Tuesday?
Jul 18 '05 #2

P: n/a
Thank you!
Python is really magic, even merge file can use "+".

On Apr 5, 2005 9:20 AM, Grant Edwards <gr****@visi.com> wrote:
On 2005-04-05, could ildg <co*******@gmail.com> wrote:
I want to merge file A and file B into a new file C, All of
them are binary.


file('C','wb').write(file('A','rb').read()+file('B ','rb').read())

--
Grant Edwards grante Yow! ... or were you
at driving the PONTIAC that
visi.com HONKED at me in MIAMI last
Tuesday?
--
http://mail.python.org/mailman/listinfo/python-list

--
鹦鹉聪明绝顶、搞笑之极,是人类的好朋友。
直到有一天,我才发觉,我是鹦鹉。
我是*墙的鹦鹉。
Jul 18 '05 #3

P: n/a
On 2005-04-05, could ildg <co*******@gmail.com> wrote:
> I want to merge file A and file B into a new file C, All of
> them are binary.


file('C','wb').write(file('A','rb').read()+file('B ','rb').read())


Python is really magic, even merge file can use "+".


You probably shouldn't use the above code for very large files,
since it reads files A and B entirely into memory before
writing the combined data to C.

For large files, something like this is probably a better idea:

fout = file('C','wb')
for n in ['A','B']:
fin = file(n,'rb')
while True:
data = fin.read(65536)
if not data:
break
fout.write(data)
fin.close()
fout.close()

--
Grant Edwards grante Yow! I feel like a wet
at parking meter on Darvon!
visi.com
Jul 18 '05 #4

P: n/a
Thank Grant, it works well.

On Apr 5, 2005 10:54 AM, Grant Edwards <gr****@visi.com> wrote:
On 2005-04-05, could ildg <co*******@gmail.com> wrote:
> I want to merge file A and file B into a new file C, All of
> them are binary.

file('C','wb').write(file('A','rb').read()+file('B ','rb').read())


Python is really magic, even merge file can use "+".


You probably shouldn't use the above code for very large files,
since it reads files A and B entirely into memory before
writing the combined data to C.

For large files, something like this is probably a better idea:

fout = file('C','wb')
for n in ['A','B']:
fin = file(n,'rb')
while True:
data = fin.read(65536)
if not data:
break
fout.write(data)
fin.close()
fout.close()

--
Grant Edwards grante Yow! I feel like a wet
at parking meter on Darvon!
visi.com
--
http://mail.python.org/mailman/listinfo/python-list

--
鹦鹉聪明绝顶、搞笑之极,是人类的好朋友。
直到有一天,我才发觉,我是鹦鹉。
我是*墙的鹦鹉。
Jul 18 '05 #5

P: n/a
Grant Edwards wrote:
For large files, something like this is probably a better idea:


Or with the little-used shutil module, and keeping your
nomenclature and block size of 65536

import shutil
fout = file('C', 'wb')
for n in ['A', 'B']:
fin = file(n, 'rb')
shutil.copyfileobj(fin, fout, 65536)
fin.close()
fout.close()
Andrew
da***@dalkescientific.com

Jul 18 '05 #6

P: n/a
I'm so glad that this this problem has so many recipes.

On Apr 5, 2005 1:57 PM, Andrew Dalke <da***@dalkescientific.com> wrote:
Grant Edwards wrote:
For large files, something like this is probably a better idea:


Or with the little-used shutil module, and keeping your
nomenclature and block size of 65536

import shutil
fout = file('C', 'wb')
for n in ['A', 'B']:
fin = file(n, 'rb')
shutil.copyfileobj(fin, fout, 65536)
fin.close()
fout.close()

Andrew
da***@dalkescientific.com

--
http://mail.python.org/mailman/listinfo/python-list

--
鹦鹉聪明绝顶、搞笑之极,是人类的好朋友。
直到有一天,我才发觉,我是鹦鹉。
我是*墙的鹦鹉。
Jul 18 '05 #7

P: n/a
On 2005-04-05, Andrew Dalke <da***@dalkescientific.com> wrote:
Grant Edwards wrote:
For large files, something like this is probably a better idea:


Or with the little-used shutil module, and keeping your
nomenclature and block size of 65536


I knew I should have looked through shutil to see if there was
already something like that.

--
Grant Edwards grante Yow! MMM-MM!! So THIS is
at BIO-NEBULATION!
visi.com
Jul 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.