Create TarFile using python

I

itzel

I have a problem. I'm new in python and I need a script that group a
list of files using Tar file utility and then, compress that block
using a compress utility (gzip i think). I already found some
information and i try to apply it, but my scripy doesn't work. The
first problem is when I had tried to open the file that i want to
group, a IO error appear:

f = open(log, "r")
IOError: [Errno 13] Permission denied: 'C:\\path"

I previusly asigned to "log" the path of the archive that i want to
group....

could sombody helpme???.
 
Y

Yu-Xi Lim

itzel said:
I have a problem. I'm new in python and I need a script that group a
list of files using Tar file utility and then, compress that block
using a compress utility (gzip i think). I already found some
information and i try to apply it, but my scripy doesn't work. The
first problem is when I had tried to open the file that i want to
group, a IO error appear:

f = open(log, "r")
IOError: [Errno 13] Permission denied: 'C:\\path"

I previusly asigned to "log" the path of the archive that i want to
group....

could sombody helpme???.

I'm going to guess that 'C:\\path' is a directory. You can't open
directories using open()/file().

There's an example in the Python Cookbook. Can't find it online (recipe
2.11 if you have the print edition), but a close example is
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/299412
 
G

Gerold Penz

itzel said:
I need a script that group a
list of files using Tar file utility and then, compress that block
using a compress utility (gzip i think).

Hi!

This script packs all files and directories inside the ``source_dir``
into the TAR-GZ-Archive (``destination``):


import os.path
import tarfile

def to_tar_gz(source_dir, destination):
"""
:param source_dir: Source directory name.
:param destination: Destination filename.
(TAR-GZ-Archive *.tar.gz)
"""

t = tarfile.open(name = destination, mode = 'w:gz')
t.add(source_dir, os.path.basename(source))
t.close()

return True


Regards,
Gerold
:)


--
________________________________________________________________________
Gerold Penz - bcom - Programmierung
(e-mail address removed) | http://gerold.bcom.at | http://sw3.at
Ehrliche, herzliche Begeisterung ist einer der
wirksamsten Erfolgsfaktoren. Dale Carnegie
 
I

itzel

yes, i did. I'm checking the link in ASPN and I think that it'll works
for "my problem" ....thanks a lot!! One more question... I'll need do
it frecuently: add more directories into the same block. Is the same
procedure?

Thanks!!!

ps. sorry about gramatic, I don't write english frecuently....


Yu-Xi Lim ha escrito:
 
Joined
Sep 8, 2009
Messages
1
Reaction score
0
compress the folder to tar gz using python.

import os.path
import tarfile
import time

def compress_folder_to_tar_gz(source_dir, tar_file_name=None):

if tar_file_name is None:
tar_file_name = source_dir

tar_file_name += '.tar.gz'

#creating archive of source directory

try:
tarFile = tarfile.open(tar_file_name, mode = 'w:gz')
tarFile.add(source_dir)
tarFile.close()

return True

except:
return None
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top