我正在尝试编写一个python程序,该程序最终将使用文件的命令行参数,确定它是否为tar或zip等文件,然后相应地提取它。我只是试图让焦油部分现在工作,我得到了多个错误。我正在检查的文件驻留在我的〜/目录中。任何想法都会很棒。第一个Python程序 - 多个错误
#!/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)
以下是错误。如果我注释掉读取错误,我也会在下一个异常中出现同样的错误。
[email protected]:~$ 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
[email protected]:~$
嗯,这真的很难让我们读。你可以看看页面右侧的格式提示吗?您可以使用'101010'按钮来正确设置代码和追溯消息的格式吗?如果我们无法阅读您的问题,我们无法提供帮助。 – 2010-01-19 17:06:27
我正在尝试,我在
块,我看到它的一切搞砸了。 – Justin 2010-01-19 17:07:29
要将文本格式设置为堆栈溢出时的代码,请将所有行加上四个空格,这就是全部,不需要用任何标签围绕文本。点击帖子上的修改,查看我是如何更改格式的。还有一个方便的工具栏按钮,您可以在选择文本时使用,以及一个热键Ctrl + K。 – 2010-01-19 17:09:43