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