Python zip파일 풀기
import zipfile
from zipfile import ZipFile
with zipfile.ZipFile(zip_file_path, 'r') as obj:
obj.namelist() # zip파일 내의 파일 이름을 가진 list 반환
obj.extract(filename, directory_name) # directory 내에 압축 해제
obj.extractall(directory_name) # directory 내에 압축 해제(없으면 생성)
with zipfile.ZipFile(zip_file_path, 'r') as obj:
zip_namelist = obj.namelist()
for name in zip_namelist :
readed = obj.open(name) # binary 형태로 읽기
if name.endswith('.csv') : # csv파일은 2가지
df = pd.read_csv(readed)
# 혹은 readed = obj.read(name); pd.read_csv(BytesIO(readed))
elif name.endswith('.png') : # Image 형태로 읽기
img = Image.open(readed)
img = img.resize((224, 224))
img_arr = np.array(img)
arr = np.append(arr, img_arr) # json 형태 읽기
elif name.endswith('json') : # json
js = json.load(readed)
length = len(js['annotations'])
참조
[Python] 압축 파일(zip) 다루기 (ft. 압축 풀지 않고 데이터 읽기)
출처: https://gmnam.tistory.com/256 [Voyager:티스토리], voyager
zipfile docs