-1

I'm having trouble figuring out how the program is supposed to read in the cvs file.

Here is the error:

Traceback (most recent call last):
  File "/Users/myname/Desktop/sealsort/sealsort.py", line 9, in <module>
    newm_path = sys.argv[1] #'./s18_new.csv'
IndexError: list index out of range
grd571
  • 1
  • 1
  • What's unclear to you about the error message? – mkrieger1 Jul 21 '22 at 19:56
  • 1
    Does this answer your question? [sys.argv\[1\], IndexError: list index out of range](https://stackoverflow.com/questions/31689100/sys-argv1-indexerror-list-index-out-of-range) – mkrieger1 Jul 21 '22 at 19:58
  • how are you running the script? are you running it from the terminal? what do you type to run it? – scotscotmcc Jul 21 '22 at 19:59
  • @scotscotmcc I'm running it in idle – grd571 Jul 21 '22 at 20:09
  • Did you read the first comment in the code? The one starting with "USE LIKE:"? – user2357112 Jul 21 '22 at 20:18
  • This program is intended to be run from the command line, as the `USE LIKE` comment at the top explains. If you're running it a different way (i.e. using Idle as you said), you will need to adapt the program. – John Gordon Jul 21 '22 at 20:18

2 Answers2

0

It seems you are trying to run this script on a macOS machine. And I'm supposing you have the three CSV files in the same folder as the Python script.

You need to navigate, via terminal, to the folder the files are stored. So, first open the Terminal application, then navigate to the folder with this command: cd /Users/myname/Desktop/sealsort (here I'm using the same path that is in your question), then you will need to execute the script as described in the first comments:

# USE LIKE:
# python sealsort.py /path/to/newm/survey /path/to/retm/survey /path/to/school/doc

Supposing the files are: s18_new.csv, s18_return.csv, s18_schools.csv, execute the script specifying the name of these files as arguments to the program. If you do not specify any of the required arguments, one of the elements at the indexes 1, 2, and 3 will not be found and you will get that error (IndexError: list index out of range).

So, the correct command would be: python sealsort.py ./s18_new.csv ./s18_return.csv ./s18_schools.csv

This way, the element at index 0 (of argv) will be sealsort.py, the element 1 will be s18_new.csv, 2 will be s18_return.csv, and 3 will be s18_schools.csv.

I hope this helps.

0

As noted in the link posted by mkrieger1, the sys.argv[1] is referencing the first command line argument. So instead of running a command like this:

python program.py

You need to run it with three arguments like this:

python program.py /path/To/first.csv /path/To/second.csv /path/To/third.csv




Note: Line 5 gives a use example

python sealsort.py /path/to/newm/survey /path/to/retm/survey /path/to/school/doc

Philip Clark
  • 316
  • 2
  • 7