biggner question

Alex Martelli aleaxit at yahoo.com
Sun Jan 28 12:50:30 EST 2001


"baasad" <baasad at qualitynet.net> wrote in message
news:952951$m4r1 at news.qualitynet.net...
> how can I read from the command line

The arguments passed to the command line when a Python script is
run are accessible as a list, named argv, in standard module sys
(which you'll need to import, of course).  The 0-th of these is the
name of the script itself.

For example, this script echoes its arguments, if any, to standard
output:

import sys
for arg in sys.argv[1:]:
    print arg,
print

If you save it as echo.py and run

C:\> python.exe echo.py un deux trois

it will output

un deux trois


> also how can I read users input :)

There are two built-in functions for that (nothing needs to be
imported), named input and raw_input.  Each takes an
optional argument (a string with which to prompt the user
for input) and returns user-input.  The input function
expects that the user will type in a Python expression, and
uses the built-in function eval to compile and run that
expression; it will return its result, or raise an exception
in case of syntax-error &c.  raw_input is simpler and more
general: it just returns the string that the user did write,
shorn of the terminating newline.  raw_input is what you
most often want.


Alex






More information about the Python-list mailing list