본문 바로가기
Language/Python

Python Zip 파일로 압축하기

by supdev 2017. 11. 9.

이 문서는 아래 링크의 문서를 참조하였습니다.

Python Zipfile Documentation : https://docs.python.org/3/library/zipfile.html


import os
import zipfile

'''
class zipfile.ZipFile(파일주소, Mode(Option))
Mode :
'r' 존재하는 파일 읽기
'w' 파일이 존재하면 지우고 생성
'a' 존재하는 파일에 append 하기
'x' 파일이 존재하지 않으면 생성, 존재하면 FileExistsError 뱉음
'''
zip = zipfile.ZipFile('c:\\python\\archive.zip', 'w')

for folder, subfolders, files in os.walk('C:\\python\\folder'):
    for file in files:
        zip.write(os.path.join(folder, file), # 파일 이름
os.path.relpath(os.path.join(folder, file), 'C:\\python\\folder'), # 압축 파일의 이름(Default : 파일 이름과 같음)
compress_type = zipfile.ZIP_DEFLATED) # 압축 타입

zip.close()


'Language > Python' 카테고리의 다른 글

Python input() 와 raw_input() 그리고 print 와 print()  (0) 2017.10.18