Last active
March 7, 2016 23:44
-
-
Save lseongjoo/d490125ba3efe5bff801 to your computer and use it in GitHub Desktop.
파이썬 파일 압축
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # coding: utf-8 | |
| """ | |
| 빅파이, 빅데이터를 위한 파이썬 | |
| facebook.com/bigpython | |
| 이성주 [email protected] | |
| >>> print('테스트1', file=open('테스트1.txt', 'w')) | |
| >>> print('테스트2', file=open('테스트2.txt', 'w')) | |
| >>> 파일목록 = ['테스트1.txt', '테스트2.txt'] | |
| >>> export_to_zip('test.zip', 파일목록) | |
| """ | |
| from zipfile import ZipFile | |
| def export_to_zip(zipfilename, files, zipmode='w', verbose=True): | |
| """파일들을 .zip 파일로 압축한다. | |
| zipfilename: '압축.zip'과 같이 생성할 파일명 | |
| files: 리스트 유형의 압축 대상 파일 목록 | |
| zipmode: 새로 생성 'w' 모드가 기본값. 'a'를 설정하면 기존 파일에 내용이 추가된다. | |
| zipfile 모듈 참조문서 | |
| https://docs.python.org/3/library/zipfile.html | |
| """ | |
| if not files: return | |
| with ZipFile(zipfilename, zipmode) as zf: | |
| for fn in files: | |
| zf.write(fn) | |
| # 압축된 파일 목록 출력 | |
| if verbose: | |
| zf.printdir() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment