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
1 1453
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: - import sys
-
print sys.argv[0] # program name
-
print sys.argv[1:] # command line arguements; if any
sys.argv is basically a list of what you typed at the command line: -
C:\Documents and Settings\UserName> myprogram.py these are some command line arguments 1 3
-
sys.argv will be: -
["myprogram.py", "these", "are", "some", "command", "line", "arguments", 1, 3]
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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;
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
|
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...
| |