Я пытаюсь написать программу на python, которая в конечном итоге примет аргумент командной строки файла, определит, является ли он файлом tar или zip и т. Д., А затем извлечет его соответствующим образом. Я просто пытаюсь заставить работать часть 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 = "xampp-linux-1.7.3a.tar.gz"
print os.getcwd()
print file
open_tar(file)
Вот ошибки. Если я закомментирую «Ошибка чтения», я получу ту же ошибку и в следующем исключении.
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:~$