Skip to content

Instantly share code, notes, and snippets.

@ujiro99
Created September 10, 2017 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ujiro99/3342614fdbf29da7a4c6d9430a44c6d1 to your computer and use it in GitHub Desktop.
Save ujiro99/3342614fdbf29da7a4c6d9430a44c6d1 to your computer and use it in GitHub Desktop.
指定ディレクトリ配下の圧縮ファイルを展開する
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import zipfile
import rarfile
import click
@click.group(invoke_without_command=True)
@click.argument('target-dir', type=click.Path(exists=True))
def cmd(target_dir: str):
__exec(target_dir)
def main():
cmd(auto_envvar_prefix='MRN')
def __exec(target_dir: str):
if os.name == 'nt':
char = "windows-1252"
else:
char = "utf8"
for d, f in __files(target_dir):
ext = os.path.splitext(f)[1]
try :
if ext == ".rar": __unrar(os.path.join(d, f), char)
if ext == ".zip": __unzip(os.path.join(d, f), char)
except Exception as e:
print(e.args)
def __files(target_dir: str):
for root, dirs, files in os.walk(target_dir):
d = root.lstrip(target_dir).lstrip('/')
for file in files:
yield (d, file)
def __unrar(rar_file: str, char: str):
print("unrar: " + rar_file)
with rarfile.RarFile(rar_file, "r", charset="utf8") as rf:
rf.extractall()
def __unzip(zip_file: str, char: str):
print("unzip: " + zip_file)
with zipfile.ZipFile(zip_file, "r") as zf:
zf.extractall()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment