473,786 Members | 2,737 Online
Bytes | Software Development & Data Engineering Community
+ 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 3552
On 11 Oct 2005 07:07:44 -0700, qu********@gmai l.com
<qu********@gma il.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***@brunningo nline.net,
http://www.brunningonline.net/simon/blog/
Oct 11 '05 #2

qu********@gmai l.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(s ys.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
7540
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 program. Strange enough that the problem never happens in C languge, i.e., in the program, if I call "foo2", it works fine. Why "foo1" doesn't work? Thanks for your answers.
3
2405
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" src="Tracking.js?someid=303" type="text/javascript"></script>
1
2168
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 = WshShell.Exec("C:\Program Files\Netop Remote Control\GUEST\ngstw32.exe /h:MTLOGUWK0009 /c:TCP/IP") end function </script>
2
1403
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
3100
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 there a workaround? template <typename T> class Base { }; template <typename U, template <typename> class TempTemp> class Derived;
4
9415
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 to a Python program under the IDLE? Thanks for you help!
8
2332
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
3847
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 bstruct, or cstruct, to the function? Sort of like this: myfunc(char * str, struct astruct *as)
24
55237
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 EventHandler(Onbutton_click); I want to pass more information related that event. & want to use that
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10164
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9961
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8989
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6745
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.