By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,898 Members | 1,183 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,898 IT Pros & Developers. It's quick & easy.

Using python code in command line with arguments

P: n/a
I have a set of python code written up in Within the code is about 5-6 different functions/sections. What I want to do is execute the python code from the Command Prompt, but only execute the parts that I want to execute. For instance, from the Command Prompt, I would enter ...\ [-fun2,fun4] . Entering this would only run section 2 and section 4 in my code. How would I do this?

Also, expanding on this, I want to enter necessary parameters for my functions that are needed. How would I do this for both the command prompt AND the python code. For example, in my code I have num=43. I would want to go to my command prompt and enter ...\ [-fun2] [-num=789]. This would run section 2 and change the num value from 43 to 789. Any help would be appreciated. Thanks
Nov 15 '10 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 2.5K+
P: 2,851
The script executes at the system command prompt when called. You should design your script to react to parameters. Example:
Expand|Select|Wrap|Line Numbers
  1. import sys
  3. def A():
  4.     print "Function 'A'"
  6. def B():
  7.     print "Function 'B'"
  9. if __name__ == "__main__":
  10.     if sys.argv[1].upper() == "A":
  11.         A()
  12.     elif sys.argv[1].upper() == "B":
  13.         B()
  14.     else:
  15.         print "Invalid parameter"
Nov 15 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.