Pipes and arguments in python

I'm new to using pipes and I/O in python, and I've been using sys.stdin.readlines() from a file.

How do you read in an additional argument? For example:

more input.txt | python program.py 200

I want to store that '200' in as a variable (int) in my program to use.

My attempt:

number = int(sys.argv[1])

For testing purposes I just typed

print number

but I didn't get the '200'.


Better explanation:

I have two files, 'test.txt' and 'test.py'

test.py

#!/usr/bin/python
import sys
line = ""
for n in [line for line in sys.stdin.readlines()]:
    print n

When running

$ more test.txt | python test.py

it will print out

asdfsdf 200

sldkfjs 100

kljslkd 300

My goal is to just print out the line with the number I pass as an argument

My attempt:

test.py

#!/usr/bin/python
import sys
number = int(sys.argv[1])
line = ""
for n in [line for line in sys.stdin.readlines()]:
    if n.split()[1] == number:
        print n

n.split()[1] should be the number from every line in the file correct? When running

$ more test.txt | python test.py 200

What I want to return is:

asdfsdf 200

But instead it's blank...


ANSWERS:


I couldn't replicate your problem.

This is my file:

#!/usr/bin/env python

import sys

number = int(sys.argv[1])
print number

Being run from command line with more input.txt | python program.py 200 and the output is 200.

You could try print str(sys.argv) to investigate exactly what is in sys.argv



 MORE:


 ? Get Input from std one char a time, all at once (C)
 ? How to parse the output of vars() neatly?
 ? Retrieve All Documents stored in Alfresco to a single file
 ? mysqli_fetch_array()/mysqli_fetch_assoc()/mysqli_fetch_row() expects parameter 1 to be resource or mysqli_result, boolean given
 ? How to deal with "transient" input elements that have no backing bean representation?
 ? Get web application ip and port on ServletContextListener
 ? JSF Composite Component script doesn't get included when it's added dynamically
 ? How to quickly Import JS, Angular, HTML etc.. files and avoid dependency issues?
 ? Why is my navbar menu cut off?
 ? PreLoad Index.HTML from another Page - Javascript