You are on page 1of 4

4/1/2014

python - sys.argv[1] meaning in script - Stack Overflow


sign up log in tour help careers 2.0

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Take the 2-minute tour

sys.argv[1] meaning in script

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

edited Nov 7 '10 at 11:44 detly 10.2k 2 22 65

asked Nov 7 '10 at 11:41 Switchkick 381 1 6 18

13 do comment not answer your question? SilentGhost Nov 7 '10 at 11:50


add comment

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

python - sys.argv[1] meaning in script - Stack Overflow


As you can see, the command-line arguments include the script name but not the interpreter name. In this sense, Python treats the script as the executable. If you need to know the name of the executable (python in this case), you can use s y s . e x e c u t a b l e. You can see from the examples that it is possible to receive arguments that do contain spaces if the user invoked the script with arguments encapsulated in quotes, so what you get is the list of arguments as supplied by the user. Now in your Python code, you can use this list of strings as input to your program. Since lists are indexed by zero-based integers, you can get the individual items using the list[0] syntax. For example, to get the script name: s c r i p t _ n a m e=s y s . a r g v [ 0 ]#t h i sw i l la l w a y sw o r k . Although that's interesting to know, you rarely need to know your script name. To get the first argument after the script for a filename, you could do the following: f i l e n a m e=s y s . a r g v [ 1 ] This is a very common usage, but note that it will fail with an IndexError if no argument was supplied. Also, Python lets you reference a slice of a list, so to get another list of just the user-supplied arguments (but without the script name), you can do u s e r _ a r g s=s y s . a r g v [ 1 : ]#g e te v e r y t h i n ga f t e rt h es c r i p tn a m e Additionally, Python allows you to assign a sequence of items (including lists) to variable names. So if you expect the user to always supply two arguments, you can assign those arguments (as strings) to two variables: u s e r _ a r g s=s y s . a r g v [ 1 : ] f u n ,g a m e s=u s e r _ a r g s#l e n ( u s e r _ a r g s )h a db e t t e rb e2 So, in final answer to your specific question, s y s . a r g v [ 1 ] represents the first command-line argument (as a s t r i n g) supplied to the script in question. It will not prompt for input, but it will fail with an IndexError if no arguments are supplied on the command-line following the script name.
answered Nov 7 '10 at 14:26 Jason R. Coombs 9,983 2 34 44 add comment

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

. / m y s c r i p t . p yf o o , the script's output will be

http://stackoverflow.com/questions/4117530/sys-argv1-meaning-in-script

2/4

4/1/2014
sys.argv is a list.

python - sys.argv[1] meaning in script - Stack Overflow

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

python - sys.argv[1] meaning in script - Stack Overflow

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