所以说我有一个合并功能.我可以将任意数量的文件合并为一个.这需要可变数量的输入.所以我的问题是,从argparser中获取参数,我如何解释2个或更多输入文件的可能性?
你可以使用nargs='+'或nargs='*'.
这些将把args收集到一个列表中.
import argparse
the_parser = argparse.ArgumentParser()
the_parser.add_argument('--input_files',nargs='+')
args = the_parser.parse_args()
Run Code Online (Sandbox Code Playgroud)
如果你想使用另一个脚本调用,subprocess你可以这样做:
import subprocess
the_files = ['to_merge_1.txt', 'to_merge_2.txt']
cmdlnargs = ['python','argparse_example.py','--input_files']
cmdlnargs.extend(the_files)
subp = subprocess.Popen(cmdlnargs,
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
stderr=subprocess.STDOUT)
stdout, stderr = subp.communicate()
print stdout
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
682 次 |
| 最近记录: |