본문으로 바로가기

 

차례

1. 관련 모듈 불러오기
2. 해제하기
3. 압축하기 
4. 읽기

 

예제

titanic.zip
0.03MB

kaggle에서 제공하는 타이타닉 파일입니다.

①train.csv, ②test.csv, ③gender_sumbisson.csv 로 구성되어 있습니다.

 

 

 


 

 

 

1. 관련 모듈 불러오기

 

import os      # 시스템 모듈
import glob    # 파일검색 모듈
import zipfile # 압축파일 모듈

 

 

 

2. 해제하기

 

(1) 개별파일 압축해제하기

 

문법

ZipFile.extract(member, path=None, pwd=None)

 

매개변수

① member = 압축해제할 개별파일의 파일명.확장자명

② path = 압축해제 대상의 작업경로. 지정하지 않는 경우 현재 디렉터리가 작업경로가 됩니다.

③ pwd = 압축해제 대상에 암호가 설정되어 있는 경우 사용합니다.

 

사용방법

with zipfile.ZipFile('titanic.zip') as f:
    f.extract(member='train.csv')
    
glob.glob("*.csv")  # csv 파일만 조회

 

(2) 모든파일 압축해제하기

 

문법

ZipFile.extractall(path=None, members=None, pwd=None)

 

매개변수

① path = 압축해제 대상의 작업경로. 지정하지 않는 경우 현재 디렉터리가 작업경로가 됩니다.

② members = 리스트 형식의 '파일명'을 넣어주면 여러 개의 파일을 선택적으로 압축해제할 수 있습니다.

③ pwd = 압축해제 대상에 암호가 설정되어 있는 경우 사용합니다

 

사용방법

with zipfile.ZipFile('titanic.zip') as f:
    f.extractall(members=['train.csv', 'test.csv'])
    
glob.glob("*.csv")  # csv 파일만 조회

 

 

 

3. 압축하기

 

my_zip = zipfile.ZipFile("C:/Users/Users/temp/test_zip.zip", 'w')  # zip파일 생성
my_zip.write('C:/Users/Users/temp/train.csv')         # 압축할 파일 write
my_zip.close()

STEP 1. zip file 생성

STEP 2. zip file에 넣고 싶은 압축할 파일을 선택합니다.

STEP 3. zip file 닫기

한개의 파일을 압축하는 경우와 여러 개의 파일을 압축하는 경우 2가지로 나눠서 설명하겠습니다.

 

(1) 한 개의 파일 압축하기

 

① 작업경로를 지정하기 위해 현재경로를 확인합니다.

import os
os.getcwd()

 

② zip file을 생성합니다.

my_zip = zipfile.ZipFile("C:/User/Users/temp/test_zip.zip", 'w')

 

③ zip file에 넣고 싶은 압축할 파일을 선택합니다.

my_zip.write('C:/Users/User/temp/train.csv')

 

③ zip file을 닫습니다.

my_zip.close()

 

(2) 여러 개의 파일 압축하기

 

한 개의 파일 압축하는 작업을 반복하는 것과 동일합니다.

방식은 위(1)과 동일하며 with문과 for문을 통해 압축하겠습니다.

files = ['train.csv', 'test.csv', 'gender_submission.csv']
with zipfile.ZipFile("titanic.zip", 'w') as my_zip:
    for i in files:
        my_zip.write(i)
    my_zip.close()

 

 

4. 읽기

 

(1) 압축파일 내 파일명 읽기 : namelist()

my_zip = zipfile.ZipFile('titanic.zip')
my_zip.namelist()

 

(2) 압축파일정보 확인 : getinfo()

zip_info = my_zip.getinfo('train.csv')   # csv파일의 Zipinfo객체 생성
print(zip_info.filename)                    # 파일명
print(zip_info.file_size)                   # 파일크기 (byte)
print(zip_info.date_time)                   # 작성일자

 

 

 


 

 

 

Python zipfile, 파이썬 폴더 압축, 파이썬 zipfile 폴더 압축, 파이썬 압축파일 읽기, 파이썬 unzip, python 7z 압축풀기, 파이썬 zipfile 경로, 파이썬 압축 파일 비밀번호, 파이썬 폴더 압축, 파이썬 특정 폴더 압축, Python ZIP 압축, 파이썬 압축풀기, Python zipfile, 파이썬 unzip, 파이썬 압축파일 읽기, 파이썬 압축 파일 비밀번호, 파이썬 unzip, python 7z 압축풀기, 파이썬 폴더 압축, Python zipfile, 파이썬 데이터압축, 파이썬 압축 파일 비밀번호, 파이썬 압축 해제 한글 깨짐, 파이썬 zipfile 경로, 리눅스 unzip, 파이썬 압축풀기, Python unzip list, 파이썬 압축 해제 한글 깨짐, Python zip 폴더 압축, 코랩 unzip, 파이썬 zipfile, 파이썬 데이터 압축, 파이썬 특정 폴더 압축, Python 폴더 압축, python zip 폴더 압축해제, Python zipfile, Jupyter notebook 압축, 파이썬 압축풀기, 파이썬 압축 파일 비밀번호, python 폴더 압축하기, 파이썬 특정 폴더 압축, Python zip 폴더 압축, Python zipfile, python zip 폴더 압축해제, 파이썬 압축풀기, 파이썬 zipfile 경로, 파이썬 압축 파일 비밀번호, 파이썬 압축파일 읽기, 파이썬 zipfile 경로, 파이썬 데이터 압축, 파이썬 unzip, 파이썬 폴더 압축, Python zipfile write, 파이썬 압축 파일 비밀번호, Python zipfile directory, Python zipfile 한글 깨짐, #Pythonzipfile #파이썬폴더압축 #파이썬zipfile폴더압축 #파이썬압축파일읽기 #파이썬unzip #python7z압축풀기 #파이썬zipfile경로 #파이썬압축파일비밀번호 #파이썬폴더압축 #파이썬특정폴더압축 #PythonZIP압축 #파이썬압축풀기 #Pythonzipfile #파이썬unzip #파이썬압축파일읽기 #파이썬압축파일비밀번호 #파이썬unzip #python7z압축풀기 #파이썬폴더압축 #Pythonzipfile #파이썬데이터압축 #파이썬압축파일비밀번호 #파이썬압축해제한글깨짐 #파이썬zipfile경로 #리눅스unzip #파이썬압축풀기 #Pythonunziplist #파이썬압축해제한글깨짐 #Pythonzip폴더압축 #코랩unzip #파이썬zipfile #파이썬데이터압축 #파이썬특정폴더압축 #Python폴더압축 #pythonzip폴더압축해제 #Pythonzipfile #Jupyternotebook압축 #파이썬압축풀기 #파이썬압축파일비밀번호 #python폴더압축하기 #파이썬특정폴더압축 #Pythonzip폴더압축 #Pythonzipfile #pythonzip폴더압축해제 #파이썬압축풀기 #파이썬zipfile경로 #파이썬압축파일비밀번호 #파이썬압축파일읽기 #파이썬zipfile경로 #파이썬데이터압축 #파이썬unzip #파이썬폴더압축 #Pythonzipfilewrite #파이썬압축파일비밀번호 #Pythonzipfiledirectory #Pythonzipfile한글깨짐