sys.argv[1], IndexError: list index out of range
sys.argv
represents the command line options you execute a script with.
sys.argv[0]
is the name of the script you are running. All additional options are contained in sys.argv[1:]
.
You are attempting to open a file that uses sys.argv[1]
(the first argument) as what looks to be the directory.
Try running something like this:
python ConcatenateFiles.py /tmp
IndexError: list index out of range: sys.argv[1] out of range
sys.argv[1]
This code expects first command line argument. If that's missing then you will encounter this error.
Example:
some_script.py
import sys
sys.argv[1]
If you dont provide myfirst_agr
(as shown below) then it will throw Index error what you are seeing.
some_script.py myfirst_agr
More examples are here
image_url = sys.argv[1] IndexError: list index out of range
sys.argv
list is populated from the command line arguments passed to the script, so in case none are provided it's expected to get an error like
Traceback (most recent call last):
File "demo4.py", line 6, in <module>
image_url = sys.argv[1]
IndexError: list index out of range
An easy repro is to put
import sys
print(sys.argv[1])
in demo.py
and run it using python demo.py
. You'll get the output
$ python demo.py
Traceback (most recent call last):
File "demo.py", line 2, in <module>
print(sys.argv[1])
IndexError: list index out of range
but if you pass an argument to it using python demo.py foo
you'll get the desired output:
$ python demo.py foo
foo
Specifically for your example, looks like when calling demo4.py
script you're not passing image url command line argument. You can pass it using python demo4.py <image_url_goes_here>
.
Python argv IndexError: list index out of range
The reason for the error is because args
only has one element, the script name, not two. If you are trying to access the script name, use args[0]
.
I got an IndexError: list index out of range in sys.argv[1]
It looks like the program is expecting 2 float command line arguments.
`
k1 = float(sys.argv[1]) # starting value of K
k2 = float(sys.argv[2])
`
So you shuld probably launch it with something like
python main.py 100 200
To go more into detail, your code is reading command line arguments and it's expecting there to be 2 of them, that can also be parsed into float values.
Normally, the first command line argument is the script file itself, so sys.argv
is always at least one element long.
That being said, you can either do as suggested above, and pass 2 floats as arguments, when launching the script, or just edit the script and hardcode 2 values instead of the ones read from the command line, like so
k1 = 100
k2 = 200
Related Topics
Reading Two Text Files Line by Line Simultaneously
Why Doesn't Django's Model.Save() Call Full_Clean()
Python How to Read N Number of Lines at a Time
Why Is the Empty Dictionary a Dangerous Default Value in Python
How to Fix "Importerror: Dll Load Failed" While Importing Win32Api
How to Use Multiple Requests and Pass Items in Between Them in Scrapy Python
How to Install a Package Inside Virtualenv
How to Set Xlim and Ylim for a Subplot in Matplotlib
Importerror: No Module Named 'Bottle' - Pycharm
How to Add Hours to Current Time in Python
Convert Alphabet Letters to Number in Python
Use of True, False, and None as Return Values in Python Functions
Replace Column Values Based on Another Dataframe Python Pandas - Better Way
Django What Is Reverse Relationship
Which Seeds Have to Be Set Where to Realize 100% Reproducibility of Training Results in Tensorflow
How to Remove Leading and Trailing Zeros in a String? Python