-2

I am trying to reproduce the results of a research article which they provided the python codes. There is a script to download their data and I am trying to run the script from terminal by,

python getData.py

an it raises the error

File "getData.py", line 127, in dataFile = sys.argv[1]+"/raw-comp.txt" IndexError: list index out of range

Related part in the python code is here,

if __name__ == '__main__' :

    dataFile = sys.argv[1]+"/raw-comp.txt"
    # Read the data

I don't know if it is related or not; but 'getData.py' script is under 'src' folder whereas 'raw-comp.txt' file is under 'data' folder.

I check out this solution here, python : IndexError: list index out of range and it says argv stores the command line arguments and you need to pass the arguments before calling it. I also checked this out; from sys import argv - what is the function of "script" where script and filename were assigned to argv. But here in this code, argv was not defined before the code piece above, besides it is the first time argv is seen in the script.

I really have no idea why it did not run, because it supposed to work without any modifying they say. Thanks already.

Edit: They have provided the description below for the script, they do not mention any argument that has to be passed from terminal.

getData: a script to download and/or build the data files.
Community
  • 1
  • 1
patti_jane
  • 3,293
  • 5
  • 21
  • 26
  • 5
    Extremely related, your program is expecting at least one argument `sys.argv[1]` and you're executing it with none. – Paulo Bu Mar 26 '14 at 22:38
  • but they do not mention about any arguments to pass. so it should be directory path, then? – patti_jane Mar 26 '14 at 22:49
  • Yes, from the code it's clear it should be the path to the directory containing raw-comp.txt, – PeterSW Mar 26 '14 at 22:50
  • 1
    _they_ were very mean because code should be documented :). Yes, that code is expecting a path, most likely a _directory_ path. – Paulo Bu Mar 26 '14 at 22:51
  • thanks for your responses, argv problem is solved! Though, it raised an another error saying 'number of white spaces must be even'. Anyways, I hope I'll figure it out. – patti_jane Mar 26 '14 at 23:01

1 Answers1

1

Try running it as:

python getData.py ../data

The presence of sys.argv[1] means the program cannot be run without passing at least one command line argument.

As documented in the python documentation sys.argv is a list of command line arguments with the first entry sys.argv[0] being the script name. The ÌndexError exception occurs when no command line arguments are passed as the code is trying to access a none existent entry.

PeterSW
  • 4,921
  • 1
  • 24
  • 35