Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Flask脚本/Python读取标准输入_Python_Flask_Stdin_Flask Script - Fatal编程技术网

使用Flask脚本/Python读取标准输入

使用Flask脚本/Python读取标准输入,python,flask,stdin,flask-script,Python,Flask,Stdin,Flask Script,现在我有一个flask脚本命令,它将路径作为参数,然后从路径读取: @manager.option('-f', '--file', dest='file_path') def my_command(file_path): open(file_path) ... 我希望它也能从标准中读取。(我经常需要在剪贴板上传递文本,每次都要创建一个文件,这很烦人。) 我怎样才能做到这一点 我已经尝试使用fileinput.input(),通过此,通过以下方式调用: cat <<

现在我有一个flask脚本命令,它将路径作为参数,然后从路径读取:

@manager.option('-f', '--file', dest='file_path')
def my_command(file_path):
     open(file_path)
     ...
我希望它也能从标准中读取。(我经常需要在剪贴板上传递文本,每次都要创建一个文件,这很烦人。)

我怎样才能做到这一点

我已经尝试使用
fileinput.input()
,通过此,通过以下方式调用:

cat << EOF | ./manage.py my_command
abc
def
ghi
EOF

cat您可以像您的示例一样执行此操作,但使用流程替换而不是管道:

./manage.py my_command <(cat <<EOF
abc
def
ghi
jkl
EOF
)
诸如此类

if(sys.argv[1] == '-'):
    f = sys.stdin
else:
    f = file(sys.argv[1])

for line in f:
    print line