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,

asked Sep 7, 2013 at 11:40
3
  • You're not passing any arguments to __init__ or rather to the myClass() call so the loop never executes... what do you expect a loop over an empty list to do? Commented Sep 7, 2013 at 11:43
  • Okay, regarding the edit - go read this and this. You seem to have a vague idea what you're doing but at the same time don't grasp some very basic concepts... Commented Sep 7, 2013 at 11:51
  • There is absolutely no need to make this a class. Commented Sep 7, 2013 at 13:04

1 Answer 1

1

You just call myClass() :

if __name__ == "__main__":
 start = myClass(sys.argv)
answered Sep 7, 2013 at 11:43
Sign up to request clarification or add additional context in comments.

2 Comments

I think this is the answer the OP wanted, but I think it should probably be said that doing your work in the __init__ method is probably a bad idea. Either move it to a method, or just make a function that does the work directly (classes are not necessary for every problem in Python).
Yes, i understand Classes are not necessary. This was just a part of learning.. I have it working without classes..

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.