Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sys.argv IndexError
#1
What is this error trying to tell me?


Traceback (most recent call last):
  File "C:\Users\renny and kite\Desktop\Python\links\links\links.py", line 8, in
 <module>
_______________________
    base  = sys.argv[1]    (this is the bad line of code)
_____________________________
IndexError: list index out of range
Press any key to continue . . .
Reply
#2
That you should run it from command line(cmd),and not as a script.
The point of sys.argv is getting stuff from command line into python.
#foo.py
import sys

script_name = sys.argv[0]
base  = sys.argv[1] #Give IndexError if not run from command line
print('Name of script <{}>'.format(script_name))
print('Argument from command line <{}>'.format(base))
From cmd python foo.py hello
Output:
Name of script <foo.py> Argument from command line <hello>
Reply
#3
Python gets the command line parameters and put them as list in sys.argv. As @snippsat showed you the first index from this list is the script name. If you don't provide any xommand line parameters to the script sys.argv holds only one element with index 0 - the script name. In that case sys.argv[1] doesn't exist.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I call sys.argv list inside a function, from the CLI? billykid999 3 787 May-02-2023, 08:40 AM
Last Post: Gribouillis
  sys.argv method nngokturk 3 1,081 Jan-23-2023, 10:41 PM
Last Post: deanhystad
  import argv Scott 3 5,889 Apr-01-2021, 11:03 AM
Last Post: radix018
  use of sys.argv deepakkr3110 3 3,036 Nov-29-2019, 05:41 AM
Last Post: buran
  Not all data returned from sys.argv ecdhyne 2 2,752 Sep-05-2019, 08:27 PM
Last Post: buran
  Pyinstaller with Sys.Argv[] - “Failed to Execute Script”? ironfelix717 0 5,299 Aug-07-2019, 02:29 PM
Last Post: ironfelix717
  I see is that sys.argv contains character strings helenharry 2 2,754 Jan-09-2019, 11:34 AM
Last Post: DeaD_EyE
  How argv works? Philia 7 4,832 Dec-26-2018, 12:20 AM
Last Post: metulburr
  sys.argv correct sintax? enricosx 3 3,193 Jul-25-2018, 09:27 AM
Last Post: buran
  I gotta problem with making argv parameters Bozx 3 4,048 Apr-04-2017, 03:44 AM
Last Post: alicarlos13

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020