urllib.request.urlretrieve()下载资源到本地

urllib.request.urlretrieve()下载资源到本地

urllib.request.urlretrieve()下载资源到本地_第1张图片
代码示例:
本实例已下载Cifair10数据集为例,下载完毕后进行加压缩包

import urllib.request as ur
import os
import sys
import tarfile
import glob
import pickle
import numpy as np
import cv2
def download_and_uncompress_tarball(tarball_url, dataset_dir):
  """Downloads the `tarball_url` and uncompresses it locally.
  Args:
    tarball_url: The URL of a tarball file.
    dataset_dir: The directory where the temporary files are stored.
  """
  filename = tarball_url.split('/')[-1]   # gqr:获取文件名
  filepath = os.path.join(dataset_dir, filename)   # gqr:拼接数据集存放路径

  # gqr:_progress为下载数据集时的回调函数,显示当前的下载进度
  def _progress(count, block_size, total_size):
    sys.stdout.write('\r>> Downloading %s %.1f%%' % (
        filename, float(count * block_size) / float(total_size) * 100.0))
    sys.stdout.flush()
  filepath, _ = ur.urlretrieve(tarball_url, filepath, _progress)   # gqr:_progress为下载数据集时的回调函数,显示当前的下载进度
  print()
  statinfo = os.stat(filepath)  # gqr: 是用来获取指定路径的状态信息,这里的指定路径可以是文件,也可以是文件夹
  print('Successfully downloaded', filename, statinfo.st_size, 'bytes.')
  tarfile.open(filepath, 'r:gz').extractall(dataset_dir)   # gqr:解压压缩包数据

DATA_URL = 'http://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz'
DATA_DIR = 'data'

download_and_uncompress_tarball(DATA_URL, DATA_DIR)   # gqr:下载数据集代码

在这里插入图片描述

你可能感兴趣的:(python)