Unzip all zip files under the current directory

I wrote it in Python because I had to unzip a lot of zip files scattered in subdirectories under the current directory. The environment I ran was Solaris 11.2, Python 2.6.2, which was not connected to the internet, so I had to do everything with only standard modules.

To get the file list, I referred to "Recursively find and output files and directories in 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])

Recommended Posts

Unzip all zip files under the current directory
Delete all pyc files under the specified directory
List all files under the current directory line by line with full path
Search for files with line feed code CR + LF under the current directory
Drop all CSV files under any directory into DataFrame
File access under the directory
[Linux] Directory under the root
Check what the character code is for all files under the directory that is Python and output
Run all unittests under a directory
Recursively unzip zip files with python
Command for the current directory Python
Read all csv files in the folder
Read all csv files in the folder
Delete all pyc files under the specified directory
Unzip all zip files under the current directory
Sort large text files
Batch convert all xlsx files in the folder to CSV files
Publish the current directory on the web server
[Unzip] How to decompress only arbitrary files from those whose directory delimiter in the zip file is backslash
Python script that makes UTF-8 files with all BOMs under the folder without BOMs
Pipfile is not created in the current directory
Access files in the same directory as the executable
Launch an HTTP server in the current directory
Current directory structure
How to list files under the specified directory in a list (multiple conditions / subdirectory search)
Checks if there is a specific character string for all files under the directory that is Python and outputs the target line
How to know the current directory in Python in Blender