0

I'm trying to untar xz/bx2/gz files in the init section of my class. I'm using the following code :

class myClass(object):
def __init__(self, *args):
    for i in args:
        try:
            f = tarfile.open(i)
            print("Extracting ", i)
            f.extractall()
            f.close()
        except tarfile.ReadError:
            print("File not a tarball, or any of .xz/.bz2/.gz archives.")
            exit()

if __name__ == "__main__":
    <???>

The only problem here is, I'm not sure what to call after "main", in order to initialize and run the init method. I've just started out, and am a bit unclear.

If I'm to write a function named unpack() which does the untarring rather than putting it under init, i know i can do something like :

if __name__ == "__main__":
    start = myClass()
    start.unpack()

Since I want to do the unpacking in init itself, how would I do it in this case ?

Edit:

Sorry in case I'm not clear, I'm trying to run this script from the command line as :

# python script.py file1.tar.bz2 file2.tar.bz2 file3.tar.bz2

So the *args should be populated with the file names, and hence the code to extract it should run, atleast from what I know.

Thank you,

4

1 回答 1

1

你只需调用 myClass() :

if __name__ == "__main__":
    start = myClass(sys.argv)
于 2013-09-07T11:43:04.027 回答