# -*- coding:utf-8 -*-
import os
import sys
import tarfile
import urllib
def download_from_url(url,dir):
file_name = url.split('/')[-1]
file_path = os.path.join(dir,file_name)
def print_progress(count, block_size, total_size):
sys.stdout.write('\r>> Downloading %s %.1f%%' % (file_name, float(count * block_size) / float(total_size) * 100.0))
sys.stdout.flush()
if not os.path.exists(dir):
os.makedirs(dir)
if not os.path.exists(file_path):
print('Start downloading...')
urllib.request.urlretrieve(url,file_path,print_progress)
else:
print('File already exists.')
return file_path
def extract_file(file_path,dest_path):
if os.path.exists(file_path):
file = tarfile.open(file_path)
file.extractall(dest_path)
file.close()
print('Extraction has been completed.')
if __name__ == '__main__':
FILE_URL = 'http://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz'
FILE_DIR = os.path.join('datasets','CIFAR10')
loaded_file_path = download_from_url(FILE_URL,FILE_DIR)
extract_file(loaded_file_path,FILE_DIR)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
Reference
[1] https://www.cnblogs.com/leokale-zz/p/11191906.html