473,473 Members | 4,185 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how to(can we ?) pass argument to .py script ?

howdy~

i wrote a .py file and it works fine, my goal is to pass argument to
that py file when it get executed, and accept that argument within py
file, eg. i prefer a command like below:

python test.py -t

and then, i may get "-t" within test.py for later use.

i have no ideas how to do and i'm really stuck, can anyone help ?

Oct 11 '05 #1
4 3540
On 11 Oct 2005 07:07:44 -0700, qu********@gmail.com
<qu********@gmail.com> wrote:
i wrote a .py file and it works fine, my goal is to pass argument to
that py file when it get executed, and accept that argument within py
file, eg. i prefer a command like below:

python test.py -t

and then, i may get "-t" within test.py for later use.

i have no ideas how to do and i'm really stuck, can anyone help ?


Short answer:

import sys
print sys.argv

You might also want to take to butchers at the optparse module.

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
Oct 11 '05 #2

qu********@gmail.com wrote:

howdy~

i wrote a .py file and it works fine, my goal is to pass argument to
that py file when it get executed, and accept that argument within py
file, eg. i prefer a command like below:

python test.py -t

and then, i may get "-t" within test.py for later use.

sys.argv and getopt module:
http://diveintopython.org/scripts_an...arguments.html

bye!

Oct 11 '05 #3
hey i got it ! tks guys !

Oct 11 '05 #4
It's quite simple. If this usage example can helps you...

### parser
try:
opts, args = getopt.getopt(sys.argv[1:], "h, e, p:", ["help"])
mode = 'reply' # default mode
pwd = ' ' # default pass (blank)
for o, a in opts:
if o == '--help' or o == '-h':
print helper()
os._exit(0)
if o == '-h':
print helper()
os._exit(0)
if o == '-e':
mode = 'echo/reply'
if o == '-p':
pwd = a
if len(args) < 3 :
print " ERR: Too much few arguments.\n Type %s -h for help." \
%(sys.argv[0])
os._exit(0)
if len(args) > 3:
print \
" ERR: Too much arguments.\n Type -h and \
look example to send argumented commands."
os._exit(0)
except:
print " ERR: Invalid Option.\n Type %s -h for help." \
%(sys.argv[0])
os._exit(0)
### /parser
Oct 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Yangang Bao | last post by:
I have a simple test program. It doesn't work. I want to know what is the reason and how to fix it. Maybe I should use template function to do it. But I don't know how. Here is the simple...
3
by: brett | last post by:
I need to pass a hard coded value to a JS file: <script language="JavaScript" src="Tracking.js" type="text/javascript"></script> Neither of these methods work: <script language="JavaScript"...
1
by: Terry Olsen | last post by:
I pulled the following client-side script from an ASP page. <script language="vbscript"> function doNetOp() Dim WshShell, oExec, HostIP Set WshShell = CreateObject("WScript.Shell") Set oExec =...
2
by: Li Pang | last post by:
Hi, I'd like to pass an address as an argument to a function, anybody knows that? Thanks in advance
11
by: Niels Dekker - no reply address | last post by:
The following attempt to pass my template "Base" as a template template argument was rejected by Microsoft VC++ 8.0 (2005), while it still works on VC++ 7.1 (2003). Is it correct C++? And is...
4
by: tony.ha | last post by:
Hello, I have open a Python program in the IDLE, but when I select the "run module" under "run" menu, it does not allow me to pass an argument to my Python program! How do you pass an argument...
8
by: laredotornado | last post by:
Hi, I want to pass my function myFunc('a', 'b', 'c') as an argument to another function. However, this will not work doStuff('x', 'y', myFunc('a', 'b', 'c'))
12
by: Fred | last post by:
Is it possible to create a function which will take any number of structures as an argument? For example, what if I wanted depending on the circumstances to pass a structure named astruct, or...
24
by: =?Utf-8?B?U3dhcHB5?= | last post by:
Can anyone suggest me to pass more parameters other than two parameter for events like the following? Event: Onbutton_click(object sender, EventArgs e)" Event handler: button.Click += new...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.