Sys.argv [1], IndexError: index index out of range
I am having a problem with the following section of Python code:
# Open/Create the output file
with open(sys.argv[1] + '/Concatenated.csv', 'w+') as outfile:
try:
with open(sys.argv[1] + '/MatrixHeader.csv') as headerfile:
for line in headerfile:
outfile.write(line + '\n')
except:
print 'No Header File'
In particular, the error is this:
Traceback (most recent call last): File "ConcatenateFiles.py", line 12, in <module> with open(sys.argv[1] + 'Concatenated.csv', 'w+') as outfile:
IndexError: list index out of range
I've done some research and it seems like it sys.argv
might require an argument on the command line when running the script, but I'm not sure what to add or what the problem might be! I also searched the site, but all the solutions I found either have no comments and / or don't include a public feature like mine.
Any help is greatly appreciated.
source to share
sys.argv
represents the command line options that you execute the script with.
sys.argv[0]
is the name of the script to use. All additional options are contained in sys.argv[1:]
.
You are trying to open a file that uses sys.argv[1]
(first argument) as what looks like a directory.
Try running something like this:
python ConcatenateFiles.py /tmp
source to share
sys.argv
is a list of command line arguments passed to the Python script, where sys.argv[0]
is the name of the script.
This is an error because you are not passing any command line argument and therefore sys.argv
has a length of 1 and is therefore sys.argv[1]
out of bounds.
To "fix", just be sure to pass the command line argument when you run the script, like
python ConcatenateFiles.py /the/path/to/the/directory
However, you will most likely want to use some sort of default directory so that it still works when you don't go through the directory:
cur_dir = sys.argv[1] if len(sys.argv) > 1 else '.'
with open(cur_dir + '/Concatenated.csv', 'w+') as outfile:
try:
with open(cur_dir + '/MatrixHeader.csv') as headerfile:
for line in headerfile:
outfile.write(line + '\n')
except:
print 'No Header File'
source to share
I have done some research and it seems that sys.argv may require an argument on the command line when running the script
It cannot, but it definitely requires . That's the whole point sys.argv
, it contains command line arguments. As with any python array, accessing a nonexistent element calls IndexError
.
Although the code is used try/except
to catch some errors, the offensive statement appears on the first line.
So the script requires a directory name and you can check if it is there by looking at len(sys.argv)
and comparing with 1 + number_of_requirements. The arvv always contains the script name plus any user-supplied options, usually a space, but the user can override the space delimiter by quoting. If the user does not provide an argument, your choices default to prompting, prompting the user, or printing a logout error.
To print an error message and exit when an argument is missing, add this line before using sys.argv for the first time:
if len(sys.argv)<2:
print "Fatal: You forgot to include the directory name on the command line."
print "Usage: python %s <directoryname>" % sys.argv[0]
sys.exit(1)
sys.argv[0]
always contains the script name and user inputs are placed in subsequent slots 1, 2, ...
see also:
source to share