Python批量处理压缩文件中数据的方法

import os
import tarfile
from six.moves import urllib
#这边的网址是从网络之间获取压缩文件。
DOWNLOAD_ROOT = "https://raw.githubusercontent.com/ageron/handson-ml/master/"
HOUSING_PATH = os.path.join("datasets", "housing")
HOUSING_URL = DOWNLOAD_ROOT + "datasets/housing/housing.tgz"
#其实实际上只需要看这个自定义函数就好了。
def fetch_housing_data(housing_url=HOUSING_URL, housing_path=HOUSING_PATH):
    os.makedirs(housing_path, exist_ok=True)
    tgz_path = os.path.join(housing_path, "housing.tgz")#用os库连接路径下压缩包
    urllib.request.urlretrieve(housing_url, tgz_path)
    housing_tgz = tarfile.open(tgz_path)
    housing_tgz.extractall(path=housing_path)
    housing_tgz.close()

猜你喜欢

转载自blog.csdn.net/lisenby/article/details/107946697
今日推荐