![](/img/trans.png)
[英]Untar file with subprocess.call is running successfully but with no effect
[英]Running python subprocess.call on tgz file to untar and stream output
我正在使用子過程調用來在命令行中解壓縮文件,我需要使用該調用的輸出將其流式傳輸到臨時文件中,這樣我才能讀取tgz文件中“ + CONTENTS”文件夾的內容。
我失敗的輸出是:
./streamContents.py rsh:ftp:沒有與主機名tar(子項)關聯的地址:ftp://myftpserver.com/pkgsrc/doxygen_pkgs/test。 tgz:無法打開:輸入/輸出錯誤tar(子級):錯誤不可恢復:現在退出
gzip:stdin:文件tar意外結束:子級返回狀態2 tar:錯誤退出由於先前的錯誤而延遲追溯(最近一次調用為最新):文件“ ./streamContents.py”,第29行,流= proc.stdout.read (8196)AttributeError:“ int”對象沒有屬性“ stdout”
#!/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()
擴展上面的評論,您需要使用urllib2
和tempfile
將tar文件下載到一個臨時文件,然后使用tarfile
打開此臨時文件。
這是一些入門代碼:
import urllib2
import tarfile
from tempfile import TemporaryFile
f_url = 'url_of_your_tar_archive'
ftpstream = urllib2.urlopen(f_url)
tmpfile = TemporaryFile()
# Download contents of tar to a temporary file
while True:
s = ftpstream.read(16384)
if not s:
break
tmpfile.write(s)
ftpstream.close()
# Access the temporary file to extract the file you need
tmpfile.seek(0)
tfile = tarfile.open(fileobj=tmpfile, mode='r:gz')
print tfile.getnames()
contents = tfile.extractfile("+CONTENTS").read()
print contents
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.