You are not logged in.

#1 2010-01-19 20:33:42

axion419
Member
Registered: 2007-04-12
Posts: 185

Python Problem - zipfile

I am just trying to write a little script that will take a tar or zip file and extract the whole thing for me.    The tar part seems to work fine.  I am unable to get the zipfile working.  I understand that I am just doing it wrong and I have not got my head around the documentation enough to figure out what.  I believe I need to create an instance of the zipfile class, then call them methods of the class.  I have did an import zipfile and then I keep trying to do it like I did the tar section.  zip =  zipfile.open(file), zip.extractall(), zip.close() . These are not working, I have found on the internet people writing things like zipfile.ZipFile.extractall(file) but that does not work as well.  I keep getting errors that say 'zipfile does not have an open method' or 'class does not have an open method'  I believe the code checking for the zip is working because I get to my 'got to opening' of the function.  Then again it could just not be giving me an error and I am chasing the wrong thing down.

Here is the code minus the zipfile bit.  If someone could just explain how to call the class or how to properly write the code without just giving me the answer that would be great.

#!/usr/bin/python

import tarfile
import os
import zipfile

def check_tar(file):
    if tarfile.is_tarfile(file):
        return True

def open_tar(file):
    try:
        tar = tarfile.open(file)
        tar.extractall()
        tar.close()
    except tarfile.ReadError:
        print "File is somehow invalid or can not be handled by tarfile"
    except tarfile.CompressionError:
        print "Compression method is not supported or data cannot be decoded"
    except tarfile.StreamError:
        print "Is raised for the limitations that are typical for stream-like TarFile objects."
    except tarfile.ExtractError:
        print "Is raised for non-fatal errors when using TarFile.extract(), but only if TarFile.errorlevel== 2."

def check_zip(file):
    if zipfile.is_zipfile(file):
        return True

def open_zip(file):
    try:
        #open the zip

        print "GOT TO OPENING"
    except zipfile.BadZipfile:
        print "The error raised for bad ZIP files (old name: zipfile.error)."
    except zipfile.LargeZipFile:
        print "The error raised when a ZIP file would require ZIP64 functionality but that has not been enabled."

rules = ((check_tar, open_tar),
         (check_zip, open_zip)
         )

def checkall(file):           
    for checks, extracts in rules:
        if checks(file):
            return extracts(file)

if __name__ == '__main__':
    file = "/home/tux/rcr311-manual.zip"
    print os.getcwd()
    print file
    checkall(file)

Last edited by axion419 (2010-01-19 20:35:18)

Offline

#2 2010-01-19 22:05:51

Bralkein
Member
Registered: 2004-10-26
Posts: 354

Re: Python Problem - zipfile

Hi, to extract the contents of the zip file you should use the following code:

zip = zipfile.ZipFile("/path/to/my/file.zip")
zip.extractall()
zip.close()

I include the following warning from the zipfile module documentation:

Warning Never extract archives from untrusted sources without prior inspection. It is possible that files are created outside of path, e.g. members that have absolute filenames starting with "/" or filenames with two dots "..".

Anyway, as you can see it is very similar to the tarfile code. The ZipFile class constructor is called, returning an instance object of the ZipFile class. The object's extractall() method is then called, at which point it dutifully extracts the contents of the file. Without wanting to make any assumptions about your level of experience, if you're not overly familiar with the concepts behind object-oriented programming, then do a bit of googling and you should be able to find some nice tutorials written from a Python perspective. I found one here here but I can't personally attest to its quality, since I already knew OOP when I learnt Python. The OOP concepts are very approachable, interesting and useful so if you haven't learned much about them I'd recommend doing a bit of reading if you plan to use Python much more.

On the other hand, if you already know all about OOP and you just had a minor brain blip, never mind then ;-)

Offline

#3 2010-01-19 23:04:03

axion419
Member
Registered: 2007-04-12
Posts: 185

Re: Python Problem - zipfile

Thanks, I will try that.  I have had some exposure to OOP in the ending of a Visual Basic class I had to take my freshman year of school and I had a hard time grasping the concept.  I understand that you create or use classes by creating instances of them. The zip = zipfile.Zipfile(/path/to/file) but after that I was stuck lol.  I will read that article you posted. Thanks again.

Offline

#4 2010-01-19 23:36:33

axion419
Member
Registered: 2007-04-12
Posts: 185

Re: Python Problem - zipfile

that worked!

Offline

Board footer

Powered by FluxBB