2

I am trying to write a python program that will eventually take a command line argument of a file, determine if its a tar or zip etc file and then exctract it accordingly. I am just trying to get the tar part working now and I am getting multiple errors. The file I am checking for resides in my ~/ directory. Any ideas would be great.

#!/usr/bin/python
import tarfile
import os
def open_tar(file):
 if tarfile.is_tarfile(file):
 try:
 tar = tarfile.open("file")
 tar.extractall()
 tar.close()
 except ReadError:
 print "File is somehow invalid or can not be handled by tarfile"
 except CompressionError:
 print "Compression method is not supported or data cannot be decoded"
 except StreamError:
 print "Is raised for the limitations that are typical for stream-like TarFile objects."
 except ExtractError:
 print "Is raised for non-fatal errors when using TarFile.extract(), but only if TarFile.errorlevel== 2."
if __name__ == '__main__':
 file = "xampp-linux-1.7.3a.tar.gz"
 print os.getcwd()
 print file
 open_tar(file)

Here are the errors. IF I comment out the Read Error, I just get teh same error on the next exception as well.

tux@crosnet:~$ python openall.py
/home/tux
xampp-linux-1.7.3a.tar.gz
Traceback (most recent call last):
 File "openall.py", line 25, in <module>
 open_tar(file)
 File "openall.py", line 12, in open_tar
 except ReadError:
NameError: global name 'ReadError' is not defined
tux@crosnet:~$ 
Lasse V. Karlsen
394k108 gold badges652 silver badges845 bronze badges
asked Jan 19, 2010 at 17:03
3
  • Well, that's really, really hard for us to read. Could you look at the formatting hints on the right side of the page? Could you then use the '101010' button to format you code and traceback messages properly? If we can't read your question, we can't help. Commented Jan 19, 2010 at 17:06
  • I am trying, I have it in <code> </code> blocks I see that its all messed up. Commented Jan 19, 2010 at 17:07
  • To format text as code on Stack Overflow, prefix all the lines with four spaces, that's all, no need to surround the text with any tags. Click edit on the post to see how I changed your formatting. There's also a handy toolbar button you can use if you select the text, and a hot-key, Ctrl+K. Commented Jan 19, 2010 at 17:09

4 Answers 4

10

You can clearly see in your error it states

NameError: global name 'ReadError' is not defined

ReadError is not a global python name. If you look at the tarfile documentation you will see ReadError is part of that modules exceptions. So in this case, you would want to do:

except tarfile.ReadError:
 # rest of your code

And you will need to do the same for the rest of those errors. Also, if all those errors will generate the same result (an error message of some sort, or a pass) you can simply do:

except (tarfile.ReadError, tarfile.StreamError) # and so on

Instead of doing them each on a seperate line. That's only if they will give the same exception

answered Jan 19, 2010 at 17:08
Sign up to request clarification or add additional context in comments.

2 Comments

Alright, thank you. I knew it was saying it was undefined, I was sure I had to prefix with though. Thanks.
Adding that stopped the errors. The other error was because I had the variable named quoted once when I did not need to do so.
2

You would need to use except tarfile.ReadError or alternatively use from tarfile import is_tarfile, open, ReadError, CompressionError, etc. and put that inside the open_tar function instead of globally.

answered Jan 19, 2010 at 17:10

Comments

1

I think you might need tarfile.ReadError rather than just ReadError?

answered Jan 19, 2010 at 17:09

Comments

1

Okay. All your exceptions (ReadError, CompressionError etc.) are inside the tarfile module and so you'll have to say except tarfile.ReadError instead of just except ReadError.

answered Jan 19, 2010 at 17:09

Comments

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.