# -*- coding: utf-8 -*-
"""
参考:https://blog.csdn.net/x2434417239/article/details/110958446
添加下载进度条显示
@author: xiao_gf
下载链接列表格式样例:
http://101.201.177.119/dlorder/orderData/xxx/Z_NAFP_C_BABJ_20191029012028_P_CLDAS_RT_CHN_0P0625_HOR-RSM000010-2019102900.nc?
Expires=xxx&OSSAccessKeyId=xxxxx&Signature=xxx&dataCode=NAFP_CLDAS2.0_RT&userId=xxx
"""
import os
import urllib.request
import sys
def _progress(block_num, block_size, total_size):
'''回调函数
@block_num: 已经下载的数据块
@block_size: 数据块的大小
@total_size: 远程文件的大小
'''
sys.stdout.write('\r>> Downloading %.1f%%' % (float(block_num * block_size) / float(total_size) * 100.0))
sys.stdout.flush()
def getnc(dataList, localPath):
if not os.path.exists(localPath): # 新建文件夹
os.mkdir(localPath)
with open(dataList,'r') as f:
lines = f.readlines()
for line in lines:
file_name = line.split('?')[0] # 文件名
file_name_1 = file_name.split('/')[-1]
print('\n')
print(file_name_1)
file_name_2 = os.path.join(localPath, file_name_1)
urllib.request.urlretrieve(line, file_name_2, _progress)
if __name__ == '__main__':
localPath= "./data/" # 下载数据路径
fl_list = os.popen('ls *.txt') # 打开一个管道
fl_list = fl_list.read().split()
for fl in fl_list:
print(fl)
getnc(fl,localPath) # 下载数据