memostack
article thumbnail
zipfile 모듈로 압축하기
Language/Python 2020. 3. 10. 10:50

zipfile 모듈은 Python 3.3 이상부터 지원해준다. 파일 압축 본 예제에서는 디렉토리와 그 하위 모든 파일들을 압축하고 있다. src_path = r"압축하고 싶은 디렉토리 경로" base_path, dir_name = os.path.split(src_path) trg_zip_name = dir_name + ".zip" cur_path = os.getcwd() # 현재 디렉토리 경로 변경 os.chdir(base_path) try: with zipfile.ZipFile(trg_zip_name, "w", zipfile.ZIP_DEFLATED) as f: for base_dir, dirs, files in os.walk(src_path): for file in files: # 상대 경로를 구한다...