Reputation: 69
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
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
Reputation: 52070
tgz
files are only gzipped tar archives. And shortcut for .tar.gz
.
tar cz
tar c
tar r
or tar u
(see the man for the difference)gzip
to compress the archive. Upvotes: 1