Skip to content Skip to sidebar Skip to footer

Reading From Standard Input Using Flask-script / Python

Right now I have flask-script command that takes a path as an argument, then reads from the path: @manager.option('-f', '--file', dest='file_path') def my_command(file_path):

Solution 1:

You could do it almost like your example, but using process substitution instead of a pipe:

./manage.py my_command <(cat <<EOF
abc
def
ghi
jkl
EOF
)

works for my simple test. . . assuming you're using bash for your shell at least. I only use bash, so don't know if this syntax works for other shells.

Alternately, you could test the value of the filename for a special value, typically - and use sys.stdin if that's the name of the file to read.

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

for line in f:
    print line

and so forth

Post a Comment for "Reading From Standard Input Using Flask-script / Python"