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

Noob question regarding sys.argv[]

TMS
100+
P: 119
I'm completely new to Python, never wrote a C program, but I have written in C++. I've never used argv, but an assignment is requiring me to learn it and use it now. I'm using Python 2.5, IDLE (for writing code), using Win XP pro environment (can't get my Ubuntu to see my wireless card).

I'm supposed to write a module that will take an argument from the command line that will read a file and process it. I really don't know how to do this and would appreciate a spoon fed version tutorial.

Help, Please?
Jan 15 '07 #1
Share this Question
Share on Google+
11 Replies


bartonc
Expert 5K+
P: 6,596
I'm completely new to Python, never wrote a C program, but I have written in C++. I've never used argv, but an assignment is requiring me to learn it and use it now. I'm using Python 2.5, IDLE (for writing code), using Win XP pro environment (can't get my Ubuntu to see my wireless card).

I'm supposed to write a module that will take an argument from the command line that will read a file and process it. I really don't know how to do this and would appreciate a spoon fed version tutorial.

Help, Please?
in a file (say myModule.py)
Expand|Select|Wrap|Line Numbers
  1. import sys
  2. for arg in sys.argv:
  3.     print arg    # the zeroth element is the module name
  4. raw_input("press any key")    # a trick to keep the python console open
(of course python must be in PATH e.v.)
from the command line (IDLE won't do this) type

python myModule.py any number of args

The raw_input() call is not needed if your command-line console does not invoke a separate window for the python process (the way that Start->Run does).
Jan 15 '07 #2

TMS
100+
P: 119
TMS
ok, so say my file name is file.txt (should it be formatted differently, like .py?). That is my arg, right? so:

python myModule.py file.txt

should call the module to call the text file, right?

I appreciate your patience. I've been trying to work this out all day, and I know it should be obvious, but for some reason it isn't.
Jan 15 '07 #3

Expert 100+
P: 511
ok, so say my file name is file.txt (should it be formatted differently, like .py?). That is my arg, right? so:

python myModule.py file.txt

should call the module to call the text file, right?

I appreciate your patience. I've been trying to work this out all day, and I know it should be obvious, but for some reason it isn't.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python
  2. import sys
  3. print "script name is: ", sys.argv[0]
  4. print "first argument file name is: " , sys.argv[1]
  5. filename = sys.argv[1] #define the file name
  6. data = open( filename ) .read() #read everything in file into memory
  7.  
Jan 15 '07 #4

bartonc
Expert 5K+
P: 6,596
ok, so say my file name is file.txt (should it be formatted differently, like .py?). That is my arg, right? so:

I appreciate your patience. I've been trying to work this out all day, and I know it should be obvious, but for some reason it isn't.
Ok, let's go slowly here... The file with your python program in it (often called a "script") gets the .py extension for many reasons:
1) This is the standard thing to do.
2) IDLE will to automatic indentation and checking, colorizing and syntax checking.
3) Windows will launch .py files using python (with no arguments).

python myModule.py file.txt

should call the module to call the text file, right?
Here, the idea of "calling" file.txt isn't quite right. Python gets "called" with arguments. The first argument is the module to run and its name can be accessed by sys.argv[0]. Arguments coming after that are simply stored in sys.argv[1:] for the script to access as it needs. So, as ghostdog shows, if it is the name of a file, that file can be "opened" from within the script.
That said, I'll modify my earlier program considering that sys.argv[0] is actually an argument to python and everthing after that (on the command line) is meant for your script.
Expand|Select|Wrap|Line Numbers
  1. # file: PyTest1.py
  2. import sys    # need to import the sys library module
  3. for arg in sys.argv[1:]    # a 'slice' prevents index errors
  4.     print arg    # these will be all of the space-separated text on the command line
  5. raw_input("press any key")
Jan 15 '07 #5

TMS
100+
P: 119
TMS
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python
  2. import sys
  3. print "script name is: ", sys.argv[0]
  4. print "first argument file name is: " , sys.argv[1]
  5. filename = sys.argv[1] #define the file name
  6. data = open( filename ) .read() #read everything in file into memory
  7.  

this is so embarrassing... OK, I'm at the command line. I've got a module named myModule.py. In it I have a combination of the previous replies to my question and they are:

Expand|Select|Wrap|Line Numbers
  1. import sys
  2.  
  3. for arg in sys.argv:
  4.     print arg    
  5.  
  6. print "script name is: ", sys.argv[0]
  7. print "first argument file name is: " , sys.argv[1]
  8. filename = sys.argv[1]
  9. data = open( filename ) .read()
  10. raw_input("press any key")    # a trick to keep the python console open
  11.  
  12.  
I go to command line, type the following:

python myModule.py someFile.txt
I get the following:
File "<stdin>", line 1
python myModule.py.py

SyntaxError: invalid syntax

What am I doing wrong... I hate being such a noob.
Jan 15 '07 #6

bartonc
Expert 5K+
P: 6,596
this is so embarrassing... OK, I'm at the command line. I've got a module named myModule.py. In it I have a combination of the previous replies to my question and they are:

Expand|Select|Wrap|Line Numbers
  1. import sys
  2.  
  3. for arg in sys.argv:
  4.     print arg    
  5.  
  6. print "script name is: ", sys.argv[0]
  7. print "first argument file name is: " , sys.argv[1]
  8. filename = sys.argv[1]
  9. data = open( filename ) .read()
  10. raw_input("press any key")    # a trick to keep the python console open
  11.  
  12.  
I go to command line, type the following:

python myModule.py someFile.txt
I get the following:
File "<stdin>", line 1
python myModule.py.py

SyntaxError: invalid syntax

What am I doing wrong... I hate being such a noob.
The problem is that you are in the Python sell, not a DOS command line.
I'd also recommend that you proceed more slowly. For example, the code I gave doesn't require any error protection. The syntax anyList[1] will cause and error if there are less than two elements in the list. Python has cool ways of dealing with this, but you are not at this stage yet (but you will be very soon).
Jan 15 '07 #7

dshimer
Expert 100+
P: 136
Let me say right off that this isn't 100% relevant so should probably be ignored until the argv questions are worked out, but for future reference it seems a reasonable place to put it in.
Originally coming from a C/C++ background I found that many programs I was re-writing wanted a reference to argc. I found it easiest to take the equivilent code and make one.

Expand|Select|Wrap|Line Numbers
  1. def argc():
  2.     '''
  3.     Should be the equivalent of the c style argc.
  4.     '''
  5.     return len(sys.argv)
  6.  
Jan 15 '07 #8

Expert 100+
P: 511
Expand|Select|Wrap|Line Numbers
  1. def argc():
  2.     '''
  3.     Should be the equivalent of the c style argc.
  4.     '''
  5.     return len(sys.argv)
  6.  
does the C++ argc mean the number of arguments? if it is, then the above
might be
Expand|Select|Wrap|Line Numbers
  1. return len(sys.argv[1:])
  2.  
because sys.argv[0] is the script name.
Jan 15 '07 #9

dshimer
Expert 100+
P: 136
Yeah, but if I remember right, like python, argv[0] is always the name of the command, so argc was always one off the number of actual argurments passed. So to keep things consistant in my mind, and in the ported code, I just kept the return value the same as it would have been.

There is no real reason to use this instead of the actual len command, it was just something I noticed early on, and mention for those coming from that background that are looking for argc or in this case the equivalent.

does the C++ argc mean the number of arguments? if it is, then the above
might be
Expand|Select|Wrap|Line Numbers
  1. return len(sys.argv[1:])
  2.  
because sys.argv[0] is the script name.
Jan 15 '07 #10

TMS
100+
P: 119
TMS
Ok, forgive my excitement, but... YEAH!!!! It works.

Thank you very much
Jan 15 '07 #11

bartonc
Expert 5K+
P: 6,596
Ok, forgive my excitement, but... YEAH!!!! It works.

Thank you very much
... YEAH!!!! It works... Believe me, we all share your exitement! Keep posting, Barton
Jan 15 '07 #12

Post your reply

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