从NOAA下载数据的python脚本

时间:2018-07-21 10:21:51

标签: python-3.x

以下是用于从NOAA中下载.sh文件格式的数据的脚本。如何使用适用于Windows操作系统的python下载相同的数据?

mkdir -p observations
cd observations

for year in `seq 2007 2016`; do
    wget https://www1.ncdc.noaa.gov/pub/data/noaa/$year/081810-99999-$year.gz
done

1 个答案:

答案 0 :(得分:0)

未经测试的安装

pip3 install wget

未经测试的代码

import errno
import os
import wget

# Take an absolute path; I've already seen horses puke! ;)
path_ = r'c:\Users\YOUR WINDOWS USERNAME\Desktop\obervations'  


def mkdir_p(path):
    try:
        os.makedirs(path)
        return 'Path created.'
    except OSError as exc:  # Python > 2.5
        if exc.errno == errno.EEXIST and os.path.isdir(path):
            return 'Path already exists!'
        else:
            # Otherwise, throw error:
            raise

mkdir_p(path_)

for year in range(2006, 2017):
    url = f'https://www1.ncdc.noaa.gov/pub/data/noaa/year/081810-99999-{year}.gz'  # Python > 3.6, otherwise use '…{}…'.format(year)
    filename = wget.download(url, out=path_)