Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
1
Help us understand the problem. What is going on with this article?
@Masahiro_T

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

More than 1 year has passed since last update.

以下の関数で出来ます

フォルダを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の権限をいじるのは少し大変だなと思ったので、別の方法でやる事にした次第です。

1
Help us understand the problem. What is going on with this article?
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
Masahiro_T
困ったときの再起動

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
1
Help us understand the problem. What is going on with this article?