티스토리 뷰

os.path.join() 경로명을 생성하는 함수

urllib.request.urlretrieve() 네트워크를 통해 해당 url의 파일을 로컬 경로로 다운로드한다. 

tarfile.open() 경로명 name에 대한 tarfile 객체를 반환한다.

extractall() tarfile 객체에 대해 압축을 해제한다. 

출처: 《핸즈온 머신러닝》 2판(자료는 멘토님이 제공해주심)


원본 코드 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import os
import tarfile
import urllib
 
DOWNLOAD_ROOT = "https://raw.githubusercontent.com/rickiepark/handson-ml2/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):
    if not os.path.isdir(housing_path):
        os.makedirs(housing_path)
    tgz_path = os.path.join(housing_path, "housing.tgz")
    urllib.request.urlretrieve(housing_url, tgz_path)
    housing_tgz = tarfile.open(tgz_path)
    housing_tgz.extractall(path=housing_path)
    housing_tgz.close()
 
fetch_housing_data()
cs

오늘은 오타로 인한 에러 없이 한번만에 코드 성공해서 기분이 좋다. 

 

폴더가 생겨 있는 것을 확인할 수 있다.