LoginSignup
2
1

More than 5 years have passed since last update.

カレントディレクトリ下のzipファイルをすべて解凍する

Posted at

カレントディレクトリ以下のサブディレクトリに散らばったzipファイルをたくさん解凍する必要があったのでPythonで書いた。
動かした環境はSolaris11.2, Python2.6.2でネットに繋がってない環境だったのですべて標準モジュールだけでやる必要があった。

ファイル一覧の取得は、「Pythonで再帰的にファイル・ディレクトリを探して出力する」を参考にさせてもらった。

unzip_all_files.py
import sys
import os
import commands

def find_all_files(directory):
    """ list-up all files in current directory(includes subdir). """
    for dir, subdirs, files in os.walk(directory):
        yield dir
        for file in files:
            yield os.path.join(dir, file)

def unzip_all_files(directory):
    """ unzip all files in current directory(includes subdir). """
    files = find_all_files(directory)
    for file in files:
        if file.endswith(u".zip") or file.endswith(u".ZIP"):
            command = u"unzip -o " + file + u" -d " + os.path.dirname(file)
            print command
            commands.getoutput(command)

if __name__ == "__main__":
    if os.path.exists(sys.argv[1]):
        unzip_all_files(sys.argv[1])
2
1
0

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
2
1