Python: 从 指定地址 下载文件 到 本地目录(附源码)

来源:互联网 发布:淘宝收藏修改分类 编辑:程序博客网 时间:2024/05/22 13:38
# -*- coding: utf-8 -*-import osimport urllibimport loggingimport syslogging.basicConfig(    format='%(asctime)s %(levelname)s %(message)s',    level=logging.INFO,    stream=sys.stdout)file_path = os.path.join(os.getcwd(),'dir_name/file_name')if not os.path.isfile(file_path):    logging.info("File doesn't exist.")    # replace with url you need    url = '网络指定地址'    # if dir 'dir_name/' doesn't exist    file_dir = file_path[:-9]    if not os.path.exists(file_dir):        logging.info("Mkdir 'dir_name/'.")        os.mkdir(file_dir)    def down(_save_path, _url):        try:            urllib.urlretrieve(_url, _save_path)        except:            print '\nError when retrieving the URL:', _save_path    logging.info("Downloading file.")    down(file_path, url)else:    logging.info("File exists.")     


  后面我会上传源代码文件托管至CSDN。



阅读全文
0 0
原创粉丝点击