我正在尝试编写一个python程序,最终将获取文件的命令行参数,确定它是否是tar或zip等文件,然后相应地对其进行exctract.我只是试图让tar部分工作,我得到了多个错误.我正在检查的文件位于我的〜/目录中.任何想法都会很棒.
#!/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 = …Run Code Online (Sandbox Code Playgroud) python ×1