473,326 Members | 2,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 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 1453
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

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

Similar topics

1
by: Dipl. -Ing. Ashu Akoachere | last post by:
Has any one got experience in using the built-in cmd class? I have derived a subclass from the base class, but it seems as if the arguments of functions using the cmd-interpreter are limited in their...
6
by: Hari | last post by:
can i have command line arguments in VS.NET applicatio? if yes how? Can i have some code snippets of the above functionality? I know we can acjieve this in console application form command...
6
by: Jon Hewer | last post by:
hi i am writing a little script and currently implementing command line arguments following the guide by mark pilgrim from dive into python; ...
7
by: Steve M | last post by:
I'm trying to invoke a Java command-line program from my Python program on Windows XP. I cannot get the paths in one of the arguments to work right. The instructions for the program describe the...
1
by: Rune Jacobsen | last post by:
Hi, I've been trying to figure this one out, but my experience just doesn't have what it takes... :| I am writing an application that reads an XML file and displays the contents in various...
40
by: raphfrk | last post by:
I have a program which reads in 3 filenames from the command line prog filename1 filename2 filename3 However, it doesn't work when one of the filenames has spaces in it (due to a directory...
2
by: Milan | last post by:
Hi, Please guide me how to set command line argument and how to retrive command line argument. Senario: vb.net application should be able to execute from command prompt by passing login and...
4
by: ohaqqi | last post by:
Hi everybody. I haven't programmed anything in about 8 years, I've read up a little bit on C and need to write a shell in C. I want to use strtok() to take an input from a user and parse it into the...
1
by: ohaqqi | last post by:
Hi guys, I'm still working on my shell. I'm trying to implement a function typefile that will take a command line input as follows: > type <file1> This command will implement a catenation of...
7
by: Jwe | last post by:
Hi, I've written a program which has both a command line interface and Windows form interface, however it isn't quite working correctly. When run from command line with no arguments it should...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.