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:~$
-
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.S.Lott– S.Lott2010年01月19日 17:06:27 +00:00Commented Jan 19, 2010 at 17:06
-
I am trying, I have it in <code> </code> blocks I see that its all messed up.Justin– Justin2010年01月19日 17:07:29 +00:00Commented 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.Lasse V. Karlsen– Lasse V. Karlsen2010年01月19日 17:09:43 +00:00Commented Jan 19, 2010 at 17:09
4 Answers 4
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
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.
Comments
I think you might need tarfile.ReadError rather than just ReadError?
Comments
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.