0


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

  • 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 Answers3

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
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