Created
September 10, 2017 11:37
-
-
Save ujiro99/3342614fdbf29da7a4c6d9430a44c6d1 to your computer and use it in GitHub Desktop.
指定ディレクトリ配下の圧縮ファイルを展開する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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