Python命令行“文件输入流”

时间:2022-04-22 21:35:08

I'm fairly new to python coming from C/C++, I was wondering how I would get my 'main.py' to reconize/use the imput given from a bash shell as:

我对来自C/ c++的python相当陌生,我想知道如何获得“main”。py'重新定义/使用来自bash shell的输入:

python main.py < text.txt

python主要。py < text.txt

(the file is in plain text)

(文件为纯文本)

3 个解决方案

#1


2  

I would use argparse to create an option parser that accepts a file path and opens it.

我将使用argparse创建一个选项解析器,该解析器接受文件路径并打开它。

import argparse

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('infile', type='open')
    args = parser.parse_args()

    for line in args.infile:
        print line

if __name__ == '__main__':
    main()

If type='open' does not provide enough control, it can be replaced with argparse.FileType('o') which accepts bufsize and mode args (see http://docs.python.org/dev/library/argparse.html#type)

如果type='open'没有提供足够的控件,可以用argparse.FileType('o')替换它,它接受bufsize和模式args(参见http://docs.python.org/dev/library/argparse.html#类型)

EDIT: My mistake. This will not support your use case. This will allow you to provide a filepath, but not pipe the file contents into the process. I'll leave this answer here as it might be useful as an alternative.

编辑:我的错误。这将不支持您的用例。这将允许您提供一个filepath,但不允许将文件内容导入进程。我把这个答案留在这里,因为它可能是一个有用的选择。

#2


8  

Read from sys.stdin:

从sys.stdin读:

import sys
sys.stdin.read()

Being a file-like object, you can use its reading functions or simply iterate over the input lines:

作为一个类似文件的对象,您可以使用它的读取函数,或者简单地遍历输入行:

for line in sys.stdin:
    print line

#3


6  

Using the fileinput module would be most appropriate here, and more flexible.

使用fileinput模块在这里是最合适的,而且更灵活。

http://docs.python.org/library/fileinput.html

http://docs.python.org/library/fileinput.html

import fileinput
for line in fileinput.input():
    process(line)

In addition to supporting stdin, it can also read from files listed as arguments.

除了支持stdin之外,它还可以从列出为参数的文件中读取数据。

#1


2  

I would use argparse to create an option parser that accepts a file path and opens it.

我将使用argparse创建一个选项解析器,该解析器接受文件路径并打开它。

import argparse

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('infile', type='open')
    args = parser.parse_args()

    for line in args.infile:
        print line

if __name__ == '__main__':
    main()

If type='open' does not provide enough control, it can be replaced with argparse.FileType('o') which accepts bufsize and mode args (see http://docs.python.org/dev/library/argparse.html#type)

如果type='open'没有提供足够的控件,可以用argparse.FileType('o')替换它,它接受bufsize和模式args(参见http://docs.python.org/dev/library/argparse.html#类型)

EDIT: My mistake. This will not support your use case. This will allow you to provide a filepath, but not pipe the file contents into the process. I'll leave this answer here as it might be useful as an alternative.

编辑:我的错误。这将不支持您的用例。这将允许您提供一个filepath,但不允许将文件内容导入进程。我把这个答案留在这里,因为它可能是一个有用的选择。

#2


8  

Read from sys.stdin:

从sys.stdin读:

import sys
sys.stdin.read()

Being a file-like object, you can use its reading functions or simply iterate over the input lines:

作为一个类似文件的对象,您可以使用它的读取函数,或者简单地遍历输入行:

for line in sys.stdin:
    print line

#3


6  

Using the fileinput module would be most appropriate here, and more flexible.

使用fileinput模块在这里是最合适的,而且更灵活。

http://docs.python.org/library/fileinput.html

http://docs.python.org/library/fileinput.html

import fileinput
for line in fileinput.input():
    process(line)

In addition to supporting stdin, it can also read from files listed as arguments.

除了支持stdin之外,它还可以从列出为参数的文件中读取数据。