python实现从网络下载文件并获得文件大小及类型的方法

yipeiwu_com5年前Python基础

本文实例讲述了python实现从网络下载文件并获得文件大小及类型的方法。分享给大家供大家参考。具体实现方法如下:

import urllib2
from settings import COOKIES
opener = urllib2.build_opener()
cookies = ";".join("%s=%s" % (k, v) for k, v in COOKIES.items())
opener.addheaders.append(('Cookie', cookies))
req = opener.open(link)
meta = req.info()
file_size = int(meta.getheaders("Content-Length")[0])
content_type = meta.getheaders('Content-Type')[0].split(';')[0]
print file_size, content_type
open(file_name, 'wb').write(req.read())

希望本文所述对大家的Python程序设计有所帮助。

相关文章

Python内置数据结构与操作符的练习题集锦

第一题: give you two var a and b, print the value of a+b, just do it! 根据提议,给出两个变量 a 和 b 并打印出 a+b...

Windows下Python2与Python3两个版本共存的方法详解

Windows下Python2与Python3两个版本共存的方法详解

前言 一向用Python 3,最近研究微信公众号开发,各云平台只支持Python 2.7,想用其他版本需要自己搭建环境。而网上又搜不到Python 3开发微信公众号的资料。暂打算先使用P...

基于python实现自动化办公学习笔记(CSV、word、Excel、PPT)

1、CSV (1)写csv文件 import csv def writecsv(path,data): with open(path, "w") as f: wri...

Python学习笔记之常用函数及说明

基本定制型 复制代码 代码如下:C.__init__(self[, arg1, ...]) 构造器(带一些可选的参数)C.__new__(self[, arg1, ...]) 构造器(带...

Python 日期区间处理 (本周本月上周上月...)

工具类 class CalendarUtils: """ 日期工具类 """ @staticmethod def delta_day(delta=0):...