函数挺有意思的,可能将来写项目会用到。
import tensorflow as tf
import os
import sys
from six.moves import urllib
import tarfile
FLAGS = tf.app.flags.FLAGS
FLAGS.data_dir = 'cifar10_data/'
DATA_URL = 'http://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz'
# Download and extract the tarball from Alex's website.
def maybe_download_and_extract():
dest_directory = FLAGS.data_dir
if not os.path.exists(dest_directory):
os.makedirs(dest_directory)
filename = DATA_URL.split('/')[-1] # 文件名
filepath = os.path.join(dest_directory, filename)
if not os.path.exists(filepath):
# 文件下载函数
def _progress(count, block_size, total_size):
# %.1f%% -> 实数后面输出1个 %
sys.stdout.write('\r>> Downloading %s %.1f%%' % (filename,
float(count * block_size) / float(total_size) * 100.0))
sys.stdout.flush() # 更新stdout
filepath, _ = urllib.request.urlretrieve(DATA_URL, filepath, _progress)
print()
statinfo = os.stat(filepath)
print('Successfully downloaded', filename, statinfo.st_size, 'bytes.')
# 提取 bin 文件路径
extracted_dir_path = os.path.join(dest_directory, 'cifar-10-batches-bin')
if not os.path.exists(extracted_dir_path):
tarfile.open(filepath, 'r:gz').extractall(dest_directory) # Read from and write to tar format archives
if __name__ == '__main__':
maybe_download_and_extract()