simple question: $1, $2 in py ?

E

es_uomikim

Hi,

I have one small simple question, that I didn't found answer in
google's. It's kind of begginers question because I'm a one. ; )

I wanned to ask how to use scripts with vars on input like this:

$ echo "something" | ./my_script.py

or:

$ ./my_scripy.py var1 var2


As far as I understand there's no $1, $2... etc stuff right ?

Sorry for a silly question.

greetz,
T
 
D

Diez B. Roggisch

As far as I understand there's no $1, $2... etc stuff right ?

Yes - but there is sys.argv

Try this


import this

print sys.argv


and invoke it as script with args. Make also sure to checkout modules
optparse and getopt to deal with the options.

Diez
 
L

Lee Harr

Hi,

I have one small simple question, that I didn't found answer in
google's. It's kind of begginers question because I'm a one. ; )

I wanned to ask how to use scripts with vars on input like this:

$ echo "something" | ./my_script.py

or:

$ ./my_scripy.py var1 var2


As far as I understand there's no $1, $2... etc stuff right ?


Others answered about commandline args... You can also read
from stdin to get the piped input:



# pi.py
import sys
i = sys.stdin.read()
print "Your input: ", i

echo "something" | python pi.py
Your input: something
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top