import urllib.request as urllib2
try:
u = urllib2.urlopen(url)
except urllib2.HTTPError:
print('File not found at: %s' % url)
sys.exit(2)
meta = u.info()
file_size = int(meta.getheaders("Content-Length")[0])
报错:HTTPMessage object has no attribute getheaders.
这是由于代码支持python2*,而当前使用的是python3*产生的这个问题。
将
file_size = int(meta.getheaders("Content-Length")[0])
改为下面代码即可:
file_size = int(meta.get("Content-Length")[0])
本文解决了一个在从URL获取文件大小时遇到的问题,即在Python 3中使用了针对Python 2的方法。具体表现为尝试调用不存在的getheaders方法。文章提供了正确的属性调用方式。
13万+

被折叠的 条评论
为什么被折叠?



