且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python 从标准输入中读取参数

更新时间:2023-11-17 23:22:16

您的 preprocess.py 文件正在尝试读取表单 sys.argv[1] 并打开它作为文件.

Your preprocess.py file is trying to read form sys.argv[1] and open it as a file.

如果您将 -h 传递给命令行,它会尝试打开具有该名称的文件.

If you pass -h to your command line, it is trying to open file with that name.

你的 preprocess 函数不应该关心命令行参数,它应该获取打开的文件描述符作为参数.

Your preprocess function shall not care about command line parameters, it shall get the open file descriptor as an argument.

所以在你解析命令行参数之后,你应该注意提供文件描述符,在你的情况下它将是sys.stdin.

So after you parse command line parameters, you shall take care about providing file descriptor, in your case it will be sys.stdin.

argparse 没有任何问题,我最喜欢的解析器是 docopt,我将用它来说明命令行解析、准备最终函数调用和最终函数调用的典型拆分.您也可以使用 argparse 实现相同的效果.

There is nothing wrong with argparse, my favourite parser is docopt and I will use it to illustrate typical split of command line parsing, preparing final function call and final function call. You can achieve the same with argparse too.

首先安装docopt:

First install docopt:

$ pip install docopt

fromstdin.py 代码如下:

"""fromstdin - Training and Testing Framework
Usage: fromstdin.py [options] <input>

Options:
    --text=<textmodel>         Text model [default: text.txt]
    --features=<features>      Features model [default: features.txt]
    --test=<testset>           Testing set [default: testset.txt]
    --vectorizer=<vectorizer>  The vectorizec [default: vector.txt]

Read data from <input> file. Use "-" for reading from stdin.
"""
import sys

def main(fname, text, features, test, vectorizer):
    if fname == "-":
        f = sys.stdin
    else:
        f = open(fname)
    process(f, text, features, test, vectorizer)
    print "main func done"

def process(f, text, features, test, vectorizer):
    print "processing"
    print "input parameters", text, features, test, vectorizer
    print "reading input stream"
    for line in f:
        print line.strip("\n")
    print "processing done"


if __name__ == "__main__":
    from docopt import docopt
    args = docopt(__doc__)
    print args
    infile = args["<input>"]
    textfile = args["--text"]
    featuresfile = args["--features"]
    testfile = args["--test"]
    vectorizer = args["--vectorizer"]
    main(infile, textfile, featuresfile, testfile, vectorizer)

可以这样称呼:

$ python fromstdin.py
Usage: fromstdin.py [options] <input>

显示帮助:

$ python fromstdin.py -h
fromstdin - Training and Testing Framework
Usage: fromstdin.py [options] <input>

Options:
    --text=<textmodel>         Text model [default: text.txt]
    --features=<features>      Features model [default: features.txt]
    --test=<testset>           Testing set [default: testset.txt]
    --vectorizer=<vectorizer>  The vectorizec [default: vector.txt]

Read data from <input> file. Use "-" for reading from stdin.

使用它,从标准输入输入:

Use it, feeding from stdin:

(so)javl@zen:~/sandbox/so/cmd$ ls | python fromstdin.py -
{'--features': 'features.txt',
 '--test': 'testset.txt',
 '--text': 'text.txt',
 '--vectorizer': 'vector.txt',
 '<input>': '-'}
processing
input parameters text.txt features.txt testset.txt vector.txt
reading input stream
bcmd.py
callit.py
fromstdin.py
scrmodule.py
processing done
main func done