Get python tarfile to skip files without read permission

Posted by chris on Stack Overflow See other posts from Stack Overflow or by chris
Published on 2010-05-20T18:02:00Z Indexed on 2010/05/20 18:20 UTC
Read the original article Hit count: 318

Filed under:
|
|

I'm trying to write a function that backs up a directory with files of different permission to an archive on Windows XP. I'm using the tarfile module to tar the directory. Currently as soon as the program encounters a file that does not have read permissions, it stops giving the error: IOError: [Errno 13] Permission denied: 'path to file'. I would like it to instead just skip over the files it cannot read rather than end the tar operation. This is the code I am using now:

def compressTar():
 """Build and gzip the tar archive."""
 folder = 'C:\\Documents and Settings'
 tar = tarfile.open ("C:\\WINDOWS\\Program\\archive.tar.gz", "w:gz")

 try:
  print "Attempting to build a backup archive"
  tar.add(folder)
 except:
  print "Permission denied attempting to create a backup archive"
  print "Building a limited archive conatining files with read permissions."

  for root, dirs, files in os.walk(folder):
   for f in files:
    tar.add(os.path.join(root, f))
   for d in dirs:
    tar.add(os.path.join(root, d))

© Stack Overflow or respective owner

Related posts about beginner

Related posts about python