Help us understand the problem. What is going on with this article?

【python】 globとzipfileだけでzipファイルを作成する関数

以下の関数で出来ます

フォルダをzipファイルにまとめる関数
import zipfile
import glob

def create_zip(folder_name):
    path_ary = glob.glob(f'{folder_name}/**', recursive=True)
    with zipfile.ZipFile(f'{folder_name}.zip', 'w', compression=zipfile.ZIP_DEFLATED) as new_zip:
        for path in path_ary:
            new_zip.write(path)

使う時はこんな感じです。

folder_name = "path/to/folder"
create_zip(folder_name) # zipファイルが作成されます

参考にしたもの:
PythonでZIPファイルを圧縮・解凍するzipfile | note.nkmk.me
Python, pathlibでファイル一覧を取得(glob, iterdir) | note.nkmk.me

なぜこれをやろうと思ったのか

Jupyter lab で、以下のコードでフォルダをzipファイルに固めようとしたら[Errno 13] Permission denied: 'swapfile'というエラーが出ました。

(このコードはここを参考にしました:Pythonでディレクトリ(フォルダ)をzipやtarに圧縮 | note.nkmk.me

import shutil

shutil.make_archive('フォルダ名', 'zip', root_dir='/')

Jupyter serverの権限をいじるのは少し大変だなと思ったので、別の方法でやる事にした次第です。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした