728x90
zipfile과 tarfile 라이브러리를 이용하여 파일 압축 해제하는 법을 알아보겠습니다.
저는 실제로는 사실 터미널 명령어를 이용해서 압축을 자주 이용하고 있습니다. (VSCode)
import zipfile
import tarfile
1. zip 파일 압축 / 압축해제
- with 구문을 활용하여 조금 더 쉽게 압축하고 압축해제 할 수 있습니다.
# 압축하기
with zipfile.ZipFile('./zipfile.zip', 'w') as myzip:
myzip.write('./file1.txt')
myzip.write('./file2.txt')
# 압축해제하기
with zipfile.ZipFile('./zipfile.zip') as myzip:
myzip.extractall()
2. tar 파일 압축 / 압축해제
- zip 파일과 비슷한 방법입니다.
# 압축하기
with tarfile.open('./zipfile.zip', 'w') as mytar:
mytar.write('./file1.txt')
mytar.write('./file2.txt')
# 압축해제하기
with tarfile.open('./zipfile.zip') as mytar:
mytar.extractall()
'프로그래밍 > Python' 카테고리의 다른 글
[파이썬, python] dataframe 정렬 (sort_values) (0) | 2023.01.20 |
---|---|
[파이썬, Python] subplot 그리기 (plt.subplot) (0) | 2023.01.19 |
[파이썬, Python] 파일 저장, 읽기 (pickle) (0) | 2023.01.18 |
[파이썬, Python] 파일 이동, 복사 (shutil) (0) | 2023.01.18 |
[파이썬, Python] 파일, 디렉토리 경로 관련 함수 (os, os.path) (0) | 2023.01.17 |