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("\n\targument error!\n")
	print("\tUseage: file_download.py https://www.xxoo/hehe.zip")
    else:
        main(sys.argv[1])

 求乱码解决方案啊!!

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

你可能感兴趣的:(python)