import os
import sys
import requests
def get_file_once(URL, FILENAME):
result = requests.get(URL)
try:
result.raise_for_status()
f = open(FILENAME,'wb')
f.write(result.content)
f.close()
print('contents of URL written to '+FILENAME)
except:
print('requests.get() returned an error code '+str(result.status_code))
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].strip()
print(file_name_1)
file_name_2 = os.path.join(localPath, file_name_1)
get_file_once(line.strip(), file_name_2)
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) # 下载数据
标签:脚本,__,name,os,localPath,gldas,file,fl,下载
From: https://www.cnblogs.com/benbenxiaofeifei/p/16850416.html