IndexError: list index out of range

5312
7
Jump to solution
05-08-2012 09:27 AM
JoanBiediger
New Contributor III
I am new to Python programming and I am trying to see if a working directory is there and if not create it.  I keep getting the following error IndexError: list index out of range.  Not sure what I am doing wrong.
I would appreciate it if anyone can lend some assistance.

import string, sys, shutil, glob, os, arcpy  #print sys.argv  print arcpy.CheckOutExtension("Spatial") from arcpy.sa import * arcpy.env.overwriteOutput = True  #check to see if scratch directory exists: if os.path.isdir(sys.argv[1] + "/working")<> True:     arcpy.CreateArcInfoWorkspace_management(sys.argv[1], "working")      if os.path.isdir(sys.argv[1] + "/output")<> True:     arcpy.CreateArcInfoWorkspace_management(sys.argv[1], "output") arcpy.AddMessage ("Output workspace created")  arcpy.env.workspace = sys.argv[1]
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor
what I really want to do is see if a folder exists in the same folder as the script and if
it doesn't then create the folder.


sys.argv[0] is the path of the script. I think this is what you're trying to do:

import sys import os import arcpy  # create a scratch folder in the same folder as the script Here = os.path.dirname(sys.argv[0]) if not arcpy.exists(os.path.join(Here,"working"):     arcpy.CreateArcInfoWorkspace_management(Here, "working")

View solution in original post

0 Kudos
7 Replies
MathewCoyle
Frequent Contributor
I'd suspect, based on your code, that you only have one argument passing to your script. In that case you would want sys.argv[0]. In Python, as in most computer languages, counting starts at 0.
0 Kudos
BruceNielsen
Occasional Contributor III
I'd suspect, based on your code, that you only have one argument passing to your script. In that case you would want sys.argv[0]. In Python, as in most computer languages, counting starts at 0.


sys.argv[0] should be the name of the script. sys.argv[1] would be the first parameter. This is different than GetParameterAsText.

Joan,

Unless you're going to be working with Arc/INFO coverages, you probably don't want to use CreateArcInfoWorkspace. Use os.mkdir to create a directory for storing shapefiles, or CreateFileGDB for a file geodatabase.
0 Kudos
MathewCoyle
Frequent Contributor
sys.argv[0] should be the name of the script. sys.argv[1] would be the first parameter. This is different than GetParameterAsText.

Joan,

Unless you're going to be working with Arc/INFO coverages, you probably don't want to use CreateArcInfoWorkspace. Use os.mkdir to create a directory for storing shapefiles, or CreateFileGDB for a file geodatabase.


Ah that's right. The only time I've used sys.argv I was doing processing using the script name as an input, that's probably how I got turned around.
0 Kudos
DarrenWiens2
MVP Honored Contributor
I keep getting the following error IndexError: list index out of range. Not sure what I am doing wrong.
Might sound silly, but the only thing I can think of is that you haven't entered a value for the parameter, sys.argv[1]. Have you (ie. when you run the script, do you indicate which folder you want?)?

Does this work? It should.
import arcpy, sys

arcpy.AddMessage(sys.argv[1])
print(sys.argv[1])
0 Kudos
JoanBiediger
New Contributor III
Hi Darren,

I think you are right, when I print the sys.argv[1] I am getting the folder and the name of the script when what I really want to do is see if a folder exists in the same folder as the script and if
it doesn't then create the folder. 

Thanks for your response, I'll keep working on it.
0 Kudos
curtvprice
MVP Esteemed Contributor
what I really want to do is see if a folder exists in the same folder as the script and if
it doesn't then create the folder.


sys.argv[0] is the path of the script. I think this is what you're trying to do:

import sys import os import arcpy  # create a scratch folder in the same folder as the script Here = os.path.dirname(sys.argv[0]) if not arcpy.exists(os.path.join(Here,"working"):     arcpy.CreateArcInfoWorkspace_management(Here, "working")
0 Kudos
JoanBiediger
New Contributor III
Hi Curtis,

Thanks for your help, that did the trick.
0 Kudos