REPOST: Re: Help - command line arguments
wurmy at earthlink.net
Sun Dec 30 06:36:20 CET 2001
Steve Zatz wrote:
> I am new to Python and although I am sure that the following is obvious I
> can't figure the following out or find an answer in any of the texts that I
> have access to.
> My question is: Can you run a script that takes command line arguments from
> within the Python shell or from the Python command line?
> For example, something like:
> test.py -a -b
> runs fine from the *Windows XP* command prompt.
> From the Python command line, I can do the following:
> import test
> which doesn't produce an exception but I can't figure out how to input the
> command line arguments.
You can mess around with sys.argv, but I'm not sure if that
>>> import sys
>>> sys.argv = 'myself.py'
>>> sys.argv[1:] = '-x foo -y bla filename'.split()
['myself.py', '-x', 'foo', '-y', 'bla', 'filename']
If the program defines a "if __name__ == "__main__"
block, you're out of luck, though. import will not execute
the code in the block.
========= WAS CANCELLED BY =======:
From: Hans Nowak <wurmy at earthlink.net>
Subject: cmsg cancel <3C2EA674.F6C69FB2 at earthlink.net>
Control: cancel <3C2EA674.F6C69FB2 at earthlink.net>
Date: Mon, 31 Dec 2001 03:38:45 GMT
Organization: A poorly-installed InterNetNews site
Message-ID: <cancel.3C2EA674.F6C69FB2 at earthlink.net>
X-Trace: news2.kornet.net 1009774376 27193 220.127.116.11 (31 Dec 2001 04:52:56 GMT)
X-Complaints-To: usenet at news2.kornet.net
NNTP-Posting-Date: Mon, 31 Dec 2001 04:52:56 +0000 (UTC)
X-Commentary: I love NewsAgent 1.10 and the Sandblaster Cancel Engine Build 74 (19 March 1999)
This message was cancelled from within Mozilla.
More information about the Python-list