python 实现文件下载

直接输入下载地址就行了,但不能下载txt、bat等文本形式,会出现乱码的情况。

#!/usr/bin/env python

import urllib2
import sys,os


def main(url):
    ext = url[url.rindex("."):]
    filename = url[url.rindex("/")+1:url.rindex(".")]
    folder = os.getcwd()
    try:
        f = urllib2.urlopen(url)
        with open(filename+ext,"wb") as code:
            code.write(f.read())
    except:
        print("parameter error!the url is error?")
        sys.exit(0)
    print("download OK,you can find it in %s\" %folder)

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("
	argument error!
")
	print("	Useage: file_download.py https://www.xxoo/hehe.zip")
    else:
        main(sys.argv[1])

 求乱码解决方案啊!!

如: https://smarthosts.googlecode.com/svn/trunk/hosts.bat 

原文地址:https://www.cnblogs.com/tk091/p/3422915.html