[Tutor] how to redirect input from pipe

Yosef Levy yosi.levy99 at gmail.com
Wed Mar 22 12:10:03 EDT 2017


Hello All,

I am running with Python 2.7
I have to run script that could have get arguments in two ways:
1. argument + file name.
2. argument + input from pipe.

example for 1:
./my_script.py -t 1,2,3 file_name.txt

example for 2:
grep snd file_name.txt | ./my_script.py -t 1,2,3

I am using "parse_known_args" to parse the rest of args when pipe exists.
and capture with: "fileinput".

My problem is that this does not run always, for second option.
Any idea how could I get standard input with additional flag?
for example, running with pipe option will be like this:
grep snd file_name.txt | ./my_script.py -t 1,2,3 -
where the additional "-" at the end will indicate script to get standard
input.

Rgds.

============================================
my script:
-----------------------------------------------------------------------
#!/usr/bin/python

import fileinput
import argparse


class TAG(object):
       pass




tag = TAG ()
parser = argparse.ArgumentParser()
parser.add_argument('-t', help = "tags,\n for example: -t 35,150,39")
args, unk = parser.parse_known_args(namespace=tag)

tag_list = tag.t.split(',')


for line in fileinput.input(unk):
         print line
-------------------------------------------------------------------------------


More information about the Tutor mailing list