472,127 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

using command line arguments

11
Hello again!

I've been trying to make my code more generic, and one of the things I want to do is to use a given file name, which can change (in the command line) instead of using a specific name.
My code looks like:

def func1
bla bla bla

def func2
bla bla bla

main code.

My questions are:
Should I name the main code "main" in order to use sys.argv?
What is the exact syntax of argv? If you want to put the first argument (as in 1, not 0) in a string variable, how do I do that?
Should I import anything, and if so, where should I locate the "import..." line? before all the functions, before the main code etc?

I have tried a python websites, but I can't find answers to those questions.


Thanks
Aug 19 '07 #1
1 1415
ilikepython
844 Expert 512MB
Hello again!

I've been trying to make my code more generic, and one of the things I want to do is to use a given file name, which can change (in the command line) instead of using a specific name.
My code looks like:

def func1
bla bla bla

def func2
bla bla bla

main code.

My questions are:
Should I name the main code "main" in order to use sys.argv?
What is the exact syntax of argv? If you want to put the first argument (as in 1, not 0) in a string variable, how do I do that?
Should I import anything, and if so, where should I locate the "import..." line? before all the functions, before the main code etc?

I have tried a python websites, but I can't find answers to those questions.


Thanks
There is no need for main (this isn't C) ;). argv is part of the system module so to use it, do something like this:
Expand|Select|Wrap|Line Numbers
  1. import sys
  2. print sys.argv[0] # program name
  3. print sys.argv[1:] # command line arguements; if any
sys.argv is basically a list of what you typed at the command line:
Expand|Select|Wrap|Line Numbers
  1. C:\Documents and Settings\UserName> myprogram.py these are some command line arguments 1 3
  2.  
sys.argv will be:
Expand|Select|Wrap|Line Numbers
  1. ["myprogram.py", "these", "are", "some", "command", "line", "arguments", 1, 3]
  2.  
Aug 19 '07 #2

Post your reply

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

Similar topics

1 post views Thread by Dipl. -Ing. Ashu Akoachere | last post: by
6 posts views Thread by Hari | last post: by
6 posts views Thread by Jon Hewer | last post: by
1 post views Thread by Rune Jacobsen | last post: by
40 posts views Thread by raphfrk | last post: by
2 posts views Thread by Milan | last post: by
reply views Thread by leo001 | last post: by

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.