1
0

More than 1 year has passed since last update.

Pythonでzipを展開する(Bandcampでダウンロードしたアルバムをアーティスト/アルバム名に展開)

Last updated at Posted at 2022-03-22

書いた理由:
Bandcampでアルバムを大量にダウンロードしたが、zip内に直でmp3が置かれている。
「アーティスト名/アルバム名」フォルダに展開したい。

ZipOpener.py
# coding: utf-8

from zipfile import ZipFile
import os
import glob

#zipファイル名は「アーティスト名 - アルバム名.zip」を想定
delimiter = " - "

current_dir_name = os.path.dirname(__file__)
file_names = glob.glob(current_dir_name + "/*.zip")

for file_name in file_names:
    print(file_name)

    artist_name, album_name = file_name[:-4].split(delimiter, 1)
    tgt_dir = os.path.join(current_dir_name, artist_name, album_name)

    with ZipFile(file_name, 'r') as zip:
        zip.printdir()
        zip.extractall(tgt_dir) 

使い方:
1.ZipOpener.pyをダウンロードしたzipファイルと同じディレクトリに置きます。
2.ZipOpener.pyを実行します。

注意点:
delimiterで区切られていないファイルがあるとエラーを吐きます。
不安な場合、zip.extractall(tgt_dir) をコメントアウトして実行するとすぐ問題のファイルが分かります。

今度やりたいこと:
マルチスレッドでやってみたい

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0