Created
December 6, 2018 00:23
-
-
Save tsuchm/4b7b935ab6528aeee78381fd6e451696 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
# -*- encoding: utf-8 -*- | |
import argparse | |
import sys | |
def parse_args(): | |
p = argparse.ArgumentParser() | |
p.add_argument('inputs', type=str, nargs='*', help='[input] text files') | |
return p.parse_args() | |
def xxx(fp): | |
# Do something | |
if __name__ == '__main__': | |
args = parse_args() | |
if len(args.inputs) > 0: | |
for file in args.inputs: | |
with open(file, 'r', encoding='utf-8') as fp: | |
xxx(fp) | |
else: | |
xxx(sys.stdin) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
このようなコードで同じような動作ができます。問題は、argparse.FileTypeはスクリプト終了まで全ファイルをopenしっぱなしっていうところですけど(これはFileTypeのissueにのってたはず、改善されたのかな……?)。
実行結果:
ちなみに、コメントアウトしている行を代わりに使うと、下のように使えます。