首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >tarfile无法打开tgz

tarfile无法打开tgz
EN

Stack Overflow用户
提问于 2017-10-09 16:59:38
回答 1查看 2.6K关注 0票数 1

我正在尝试从这个网站下载tgz文件:https://plg.uwaterloo.ca/cgi-bin/cgiwrap/gvcormac/foo07

这是我的剧本:

代码语言:javascript
复制
import os
from six.moves import urllib
import tarfile

spam_path=os.path.join('ML', 'spam')
root_download='https://plg.uwaterloo.ca/cgi-bin/cgiwrap/gvcormac/foo07'
spam_url=root_download+'255 MB Corpus (trec07p.tgz)'

if not os.path.isdir(spam_path):
    os.makedirs(spam_path)

path=os.path.join(spam_path, 'trec07p.tgz')
if not os.path.isfile('trec07p.tgz'):
    urllib.request.urlretrieve(spam_url,path)
tar_file=tarfile.open(path)

我不确定我遗漏了什么,但返回了以下错误:

代码语言:javascript
复制
---------------------------------------------------------------------------
ReadError                                 Traceback (most recent call last)
<ipython-input-21-5644813e0670> in <module>()
     18 if not os.path.isfile('trec07p.tgz'):
     19     urllib.request.urlretrieve(spam_url,path)
---> 20 tar_file=tarfile.open(path)
     21 # tar_file.extractall(path)
     22 # tar_file.close()

/anaconda/lib/python2.7/tarfile.pyc in open(cls, name, mode, fileobj, bufsize, **kwargs)
   1678                         fileobj.seek(saved_pos)
   1679                     continue
-> 1680             raise ReadError("file could not be opened successfully")
   1681 
   1682         elif ":" in mode:

ReadError: file could not be opened successfully

提前感谢您的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-09 17:06:24

您可以向tarfile.open添加其他参数。您需要将模式设置为'r:gz'

代码语言:javascript
复制
tarfile.open(path, 'r:gz')

接受协议后的工作示例

代码语言:javascript
复制
import tarfile

import requests

URL = 'https://plg.uwaterloo.ca/cgi-bin/cgiwrap/gvcormac/trec07p.tgz'
FILE = '/home/blake/Downloads/trec07p.tgz'

resp = requests.get(URL, stream=True)
resp.raise_for_status()

with open(FILE, 'wb') as out_file:
    for line in resp.iter_content(chunk_size=1024*4, decode_unicode=False):
        out_file.write(line)


f = tarfile.open(FILE, 'r:gz')
print(f.getnames())

f.close()

输出:

代码语言:javascript
复制
['trec07p/data/inmail.35059',
 'trec07p/data/inmail.34430',
 'trec07p/data/inmail.45722',
 ..
 ..]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46651490

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档