![](http://python-guide.work/wp-content/themes/cocoon-master/images/b-woman.png)
Pythonで
・フォルダをZIP形式で圧縮
できます!
※ここで紹介するのは「パスワード無しZIPファイル」の作成(圧縮)方法です。
コード
ここでは例として
・デスクトップ配下の約1GBのフォルダ「test」を圧縮し
・フォルダ「output」配下にファイル「test.zip」として作成
します。
![フォルダ「test」](https://python-guide.work/wp-content/uploads/2023/08/py_zipfile_make_archive_001.png)
![フォルダ「test」のサイズは約1GB](https://python-guide.work/wp-content/uploads/2023/08/py_zipfile_make_archive_002.png)
import shutil
# ZIP形式で圧縮するフォルダパス
target_path = r'C:\Users\lunch\Desktop\test'
# 作成先フォルダ
dest_folder_path = r'C:\Users\lunch\Desktop\output'
# ZIPファイル名
zip_file_name = 'test'
# ZIP形式で圧縮
shutil.make_archive(dest_folder_path + '\\' + zip_file_name, format='zip', root_dir=target_path)
実行結果
フォルダをZIP形式で圧縮できました。
![実行結果①](https://python-guide.work/wp-content/uploads/2023/08/py_zipfile_make_archive_003.png)
![実行結果②](https://python-guide.work/wp-content/uploads/2023/08/py_zipfile_make_archive_005.png)
![実行結果③](https://python-guide.work/wp-content/uploads/2023/08/py_zipfile_make_archive_006.png)
参考
上記で使用した以下の詳細は、公式サイトをご確認ください。
●標準ライブラリ「shutil」モジュールの「make_archive」