I have a binary file (.bin) and a (.txt) file.
Using Python3, is there any way to combine these two files into one file (WITHOUT using any compressor tool if possible)?
And if I have to use a compressor, I want to do this with python.
As an example, I have 'file.txt' and 'file.bin', I want a library that gets these two and gives me one file, and also be able to un-merge the file.
Thank you
Asked
Active
Viewed 378 times
0

Erfan Sh.z.
- 59
- 7
-
you should specify what do you exactly mean by a merge – pkacprzak Jan 01 '14 at 14:41
-
I just want my program to output one file. so i want to stick them together and be able to separate them again – Erfan Sh.z. Jan 01 '14 at 14:43
-
there are as much solutions to this problem as much compression-libs are on the market for python! have you tried any of them? – LPH Jan 01 '14 at 14:51
-
I dont want to compress files,I just want to put them in one file without decreasing their sizes.My program is a Huffman encrypter itself! – Erfan Sh.z. Jan 01 '14 at 14:58
3 Answers
0
Just create a tar
archive, a module that let's you accomplish this task is already bundled with Cpython, and it's called tarfile
.
more examples here.

Community
- 1
- 1

user2485710
- 9,451
- 13
- 58
- 102
-
thank you, but I dont want any library to compress my files, actually my program is meant to compress a .txt file! – Erfan Sh.z. Jan 01 '14 at 14:54
-
@ErfanSharafzadeh it's a module that comes with a standard python interpreter, there is no use of no "extra" library here . – user2485710 Jan 01 '14 at 14:56
-
0
there are a lot of solutions for compressing! gzip or zlib would allows compression and decompression and could be a solution for your problem.
Example of how to GZIP compress an existing file from [http://docs.python.org]:
import gzip
f_in = open('file.txt', 'rb')
f_out = gzip.open('file.txt.gz', 'wb')
f_out.writelines(f_in)
f_out.close()
f_in.close()
but also tarfile is a good solution!

LPH
- 1,275
- 9
- 16
0
Tar's the best solution to get binary file. If you want the output to be a text, you can use base64 to transform binary file into a text data, then concatenate them into one file (using some unique string (or other technique) to mark the point they were merged).

Community
- 1
- 1

Satanowski
- 16
- 1
-
I don't need to merge them because it increases the output file size(which is very important in my case) – Erfan Sh.z. Jan 01 '14 at 15:04