Professional Documents
Culture Documents
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
I'm currently teaching myself Python and was just wondering (In reference to my example below) in simplified terms what the sys.argv[1] represents. Is it simply asking for an input? # ! / u s r / b i n / p y t h o n 3 . 1 #i m p o r tm o d u l e su s e dh e r e-s y si sav e r ys t a n d a r do n e i m p o r ts y s #G a t h e ro u rc o d ei nam a i n ( )f u n c t i o n d e fm a i n ( ) : p r i n t( ' H e l l ot h e r e ' ,s y s . a r g v [ 1 ] ) #C o m m a n dl i n ea r g sa r ei ns y s . a r g v [ 1 ] ,s y s . a r g v [ 2 ]. . #s y s . a r g v [ 0 ]i st h es c r i p tn a m ei t s e l fa n dc a nb ei g n o r e d #S t a n d a r db o i l e r p l a t et oc a l lt h em a i n ( )f u n c t i o nt ob e g i n #t h ep r o g r a m . i f_ _ n a m e _ _= =' _ _ m a i n _ _ ' : m a i n ( )
python
6 Answers
I note that previous answers made lots of assumptions about the user's knowledge. This answer attempts to answer the question at a more tutorial level. For every invocation of Python, s y s . a r g v is automatically a list of strings representing the arguments (as separated by spaces) on the command-line. The name comes from the C programming convention in which argv and argc represent the command line arguments. You'll want to learn more about lists and strings as you're familiarizing yourself with Python, but it the meantime, here are a few things to know. You can simply create a script that prints the arguments as they're represented. It also prints the number of arguments, using the l e n function on the list. f r o m_ _ f u t u r e _ _i m p o r tp r i n t _ f u n c t i o n i m p o r ts y s p r i n t ( s y s . a r g v ,l e n ( s y s . a r g v ) ) The script requires Python 2.6 or later. If you call this script p r i n t _ a r g s . p y, you can invoke it with different arguments to see what happens. >p y t h o np r i n t _ a r g s . p y [ ' p r i n t _ a r g s . p y ' ]1 >p y t h o np r i n t _ a r g s . p yf o oa n db a r [ ' p r i n t _ a r g s . p y ' ,' f o o ' ,' a n d ' ,' b a r ' ]4 >p y t h o np r i n t _ a r g s . p y" f o oa n db a r " [ ' p r i n t _ a r g s . p y ' ,' f o oa n db a r ' ]2 >p y t h o np r i n t _ a r g s . p y" f o oa n db a r "a n db a z [ ' p r i n t _ a r g s . p y ' ,' f o oa n db a r ' ,' a n d ' ,' b a z ' ]4
http://stackoverflow.com/questions/4117530/sys-argv1-meaning-in-script
1/4
4/1/2014
sys.argv[1] contains the first command line argument passed to your script. For example, if your script is named h e l l o . p y and you do: $p y t h o n 3 . 1h e l l o . p yf o o or: $c h m o d+ xh e l l o . p y #m a k es c r i p te x e c u t a b l e $. / h e l l o . p yf o o Your script will print: H e l l ot h e r ef o o
edited Nov 7 '10 at 21:47 answered Nov 7 '10 at 11:44 Frdric Hamidi 113k 12 165 235
add comment
Just adding to Frederic's answer, for example if you call your script as follows: . / m y s c r i p t . p yf o ob a r s y s . a r g v [ 0 ] would be "./myscript.py" s y s . a r g v [ 1 ] would be "foo" and s y s . a r g v [ 2 ] would be "bar" ... and so forth. In your example code, if you call the script as follows "Hello there foo".
answered Nov 7 '10 at 11:59 CodeWombat 222 1 4 19 add comment
http://stackoverflow.com/questions/4117530/sys-argv1-meaning-in-script
2/4
4/1/2014
sys.argv is a list.
This list is created by your command line, it's a list of your command line arguments. For example: in your command line you input something like this, python3.2 file.py something sys.argv will become a list ['file.py', 'something'] In this case sys.argv[1] = 'something'
answered Mar 25 '13 at 1:34 user2205939 41 1 add comment
sys.argv is a list containing the script path and command line arguments; i.e. sys.argv[0] is the path of the script you're running and all following members are arguments.
answered Nov 7 '10 at 11:53 TartanLlama 557 5 11 add comment
Adding a few more points to Jason's Answer : For taking all user provided arguments : u s e r _ a r g s=s y s . a r g v [ 1 : ] Consider the sys.argv as a list of strings as (mentioned by Jason). So all the list manipulations will apply here. This is called "List Slicing". For more info visit here. The syntax is like this : list[start:end:step]. If you omit start, it will default to 0, and if you omit end, it will default to length of list. Suppose you only want to take all the arguments after 3rd argument, then : u s e r _ a r g s=s y s . a r g v [ 3 : ] Suppose you only want the first two arguments, then : u s e r _ a r g s=s y s . a r g v [ 0 : 2 ] o r u s e r _ a r g s=s y s . a r g v [ : 2 ] Suppose you want arguments 2 to 4 : u s e r _ a r g s=s y s . a r g v [ 2 : 4 ] Suppose you want the last argument (last argument is always -1, so what is happening here is we start the count from back. So start is last, no end, no step) : u s e r _ a r g s=s y s . a r g v [ 1 ] Suppose you want the second last argument : u s e r _ a r g s=s y s . a r g v [ 2 ] Suppose you want the last two arguments : u s e r _ a r g s=s y s . a r g v [ 2 : ] Suppose you want the last two arguments. Here, start is -2, that is second last item and then to the end (denoted by ":") : u s e r _ a r g s=s y s . a r g v [ 2 : ] Suppose you want the everything except last two arguments. Here, start is 0 (by default), and end is second last item : u s e r _ a r g s=s y s . a r g v [ : 2 ] Suppose you want the arguments in reverse order : u s e r _ a r g s=s y s . a r g v [ : : 1 ] Hope this helps.
edited Oct 16 '13 at 11:04 answered Oct 16 '13 at 10:20 Rahul 11 2
add comment
http://stackoverflow.com/questions/4117530/sys-argv1-meaning-in-script
3/4
4/1/2014
Not the answer you're looking for? Browse other questions tagged python or ask your own question.
http://stackoverflow.com/questions/4117530/sys-argv1-meaning-in-script
4/4