从“内容长度"获取文件大小. python 3.2中文件中的值 [英] Get file size from "Content-Length" value from a file in python 3.2

查看:309
本文介绍了从“内容长度"获取文件大小. python 3.2中文件中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从元变量中获取Content-Length值.我需要获取要下载的文件的大小.但是最后一行返回错误,HTTPMessage对象没有属性getheaders.

I want to get the Content-Length value from the meta variable. I need to get the size of the file that I want to download. But the last line returns an error, HTTPMessage object has no attribute getheaders.

import urllib.request
import http.client

#----HTTP HANDLING PART----
 url = "http://client.akamai.com/install/test-objects/10MB.bin"

file_name = url.split('/')[-1]
d = urllib.request.urlopen(url)
f = open(file_name, 'wb')

#----GET FILE SIZE----
meta = d.info()

print ("Download Details", meta)
file_size = int(meta.getheaders("Content-Length")[0])

推荐答案

您似乎正在使用Python 3,并且已阅读Python 2.x的一些代码/文档.它的文献资料很少,但是Python 3中没有getheaders方法,只有一个get_all方法.

It looks like you are using Python 3, and have read some code / documentation for Python 2.x. It is poorly documented, but there is no getheaders method in Python 3, but only a get_all method.

请参见此错误报告.

这篇关于从“内容长度"获取文件大小. python 3.2中文件中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆