creating a tar file with python

John McMonagle jmcmonagle at velseis.com.au
Fri Aug 24 00:20:49 EDT 2007


Brian McCann wrote:
> Hi,
>  
> I'm trying to create a tar file of the contents of the current directory
>  
> right now there is only one file "text.xml" in the current dir,  I'm 
> using"." current dir as source
> but that gives syntax error
> 
> any help would be greatly appreciated
> --Brian
>  
>  
> #!/usr/bin/python
> import string
> import os
> import sys
> import time
> import errno
> import shutil
> import tarfile
>  
> 
> tar = tarfile.open(.,"test.tar.gz", "w:gz)

Contents of current directory in a new gzipped tarfile (includes 
"hidden" *nix files):


import tarfile, glob
t = tarfile.open('tarfilename.tar.gz', 'w:gz')
for filename in glob.glob('*')+glob.glob('.*'):
     t.add(filename)
t.close()

Note: this includes any subdirectories and their files below the current 
level.

Regards,

John






More information about the Python-list mailing list