Discussion:
How to merge two binary files into one?
Grant Edwards
2005-04-05 13:40:15 UTC
Permalink
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
could ildg
2005-04-05 07:33:29 UTC
Permalink
I'm so glad that this this problem has so many recipes.
Or with the little-used shutil module, and keeping your
nomenclature and block size of 65536
import shutil
fout = file('C', 'wb')
fin = file(n, 'rb')
shutil.copyfileobj(fin, fout, 65536)
fin.close()
fout.close()
Andrew
dalke at dalkescientific.com
--
http://mail.python.org/mailman/listinfo/python-list
--
????????????????????
????????????????
????????
Grant Edwards
2005-04-05 01:20:08 UTC
Permalink
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?
could ildg
2005-04-05 04:47:11 UTC
Permalink
Thank Grant, it works well.
Post by Grant Edwards
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.
fout = file('C','wb')
fin = file(n,'rb')
data = fin.read(65536)
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
--
????????????????????
????????????????
????????
Andrew Dalke
2005-04-05 05:57:30 UTC
Permalink
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
dalke at dalkescientific.com
Grant Edwards
2005-04-05 02:54:41 UTC
Permalink
Post by Grant Edwards
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
could ildg
2005-04-05 02:06:58 UTC
Permalink
Thank you!
Python is really magic, even merge file can use "+".
Post by Grant Edwards
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
--
????????????????????
????????????????
????????
could ildg
2005-04-05 01:12:17 UTC
Permalink
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.
--
????????????????????
????????????????
????????
Continue reading on narkive:
Loading...