user2911701
user2911701

Reputation: 69

How can I convert .tar to .tgz

I'm trying to compress some files with python. I'm using subprocess, the problem is that I don't know how to compress all the files. Here is what I do:

comand = czf
saveFolder = /home/albert/a.tgz
temporal = /home/albert/test1

subprocess.call(["tar",comand,saveFolder,temporal])

How can I add more files? If i try to do this, it sends me error:

subprocess.call(["tar",comand,saveFolder,temporal1,temporal2])

subprocess.call(["tar",comand,saveFolder,temporal + temporal2])

I also tryed to use:

subprocess.call(["tar",comand,saveFolder,temporal])

and then add all files into the .tar file

subprocess.call(["tar","rvf",saveFolder,temporal])

The problem with that last idea is that I can just do it for .tar files, but with .gzip or other types it doesn't work.

What I can do? Thanks.

Upvotes: 1

Views: 1537

Answers (2)

Mauro Baraldi
Mauro Baraldi

Reputation: 6575

What if you use only Python to do the dirty job?

Creating a tar package from entire dir (not compressed)

>>> import tarfile
>>> tar = tarfile.open('package.tar', 'w')
>>> tar.add('full/path/of/dir/')
>>> tar.close()

Updating a tar package.

Obs.: You can't update compressed package. It must be decompressed, updated, them re-compressed

>>> tar = tarfile.open('package.tar', 'w')
>>> tar.add('new_file.txt')
>>> tar.add('new_other_file.txt')
>>> tar.close()

Creating a tar package compressed

>>> tar = tarfile.open('package.tar.gz', 'w:gz')
>>> tar.add('server.db')
>>> tar.add('readme.txt')
>>> tar.close()

Upvotes: 2

Sylvain Leroux
Sylvain Leroux

Reputation: 52070

tgz files are only gzipped tar archives. And shortcut for .tar.gz.

  • You can create and compress an archive using tar cz
  • You can create a (non compressed) archive using tar c
  • You can append to a (non compressed) archive using tar r or tar u (see the man for the difference)
  • You can use gzip to compress the archive.
  • But, you cannot (at least with GNU tar) update a compressed archive.

Upvotes: 1

Related Questions