您的位置:首页 > 编程语言 > Python开发

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

2017-06-27 17:08 453 查看
# -*- coding: utf-8 -*-

import os
import urllib
import logging
import sys

logging.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。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: