我正在使用子进程调用在命令行中解压文件,我需要使用该调用的输出流入临时文件,以便我可以用tgz文件读取“+ CONTENTS”文件夹的内容。在tgz文件上运行python subprocess.call来解压和流输出
我的失败输出是:
./streamContents.py RSH:FTP:没有与主机名 焦油(孩子)相关地址:ftp://myftpserver.com/pkgsrc/doxygen_pkgs/test。 TGZ:无法打开:输入/输出错误 焦油(子):错误是不可恢复的:现在退出
的gzip:标准输入:返回儿童状况2 焦油:文件 焦油意外结束错误退出之前的延迟错误 回溯(最近最后一次通话): 文件 “./streamContents.py”,第29行,在 流= proc.stdout.read(8196) AttributeError的: '诠释' 对象有没有属性 '标准输出'
#!/usr/bin/python
from io import BytesIO
import urllib2
import tarfile
import ftplib
import socket
import threading
import subprocess
tarfile_url = "ftp://myftpserver.com/pkgsrc/doxygen_pkgs/test.tg
z"
try:
ftpstream = urllib2.urlopen(tarfile_url)
except URLerror, e:
print "URL timeout"
except socket.timeout:
print "Socket timeout"
# BytesIO creates an in-memory temporary file.
tmpfile = BytesIO()
last_size = 0
tfile_extract = ""
while True:
proc = subprocess.call(['tar','-xzvf', tarfile_url], stdout=subprocess.PIPE)
# Download a piece of the file from the ftp connection
stream = proc.stdout.read(8196)
if not stream: break
tmpfile.write(bytes(stream))
# Seeking back to the beginning of the temporary file.
tmpfile.seek(0)
# r|gz forbids seeking backward; r:gz allows seeking backward
try:
tfile = tarfile.open(fileobj=tmpfile, mode="r:gz")
print tfile.extractfile("+CONTENTS")
tfile_extract_text = tfile_extract.read()
print tfile_extract.tell()
tfile.close()
if tfile_extract.tell() > 0 and tfile_extract.tell() == last_size:
print tfile_extract_text
break
else:
last_size = tfile_extract.tell()
except Exception:
tfile.close()
pass
tfile_extract_text = tfile_extract.read()
print tfile_extract_text
# When you're done:
tfile.close()
tmpfile.close()
你为什么要反复在循环中调用焦油? –
对不起,我没有注意到,我看到这是我的问题的一部分。最初我试图直接从tar文件流。 tarfile模块不会让我直接从它流,因为它需要建立索引之前,让我流。 – rayray84
另外,在ftp URL上运行'tar'似乎是错误的。您需要将文件保存到磁盘并在本地文件上运行'tar'。 – vikramls