Select to view content in your preferred language

Py 2.4 - os.walk and using sys.argv

1228
8
02-07-2012 05:17 AM
DebraGajdos
Deactivated User
Hi everyone,

I have a script that walks through a multi-level directory. I would like to be able to use sys.argv to call the workspace, and run thru and do the process.  The script works when I hard code to the top level directory.  When I add the sys argv, It doesn't run.

import arcgisscripting, sys, os

gp = arcgisscripting.create()

try:
    def treeDir (arg, dirpath, basenames):
        gp.workspace = dirpath
        lines_list = []
        for f in basenames:
             if f == "lines.shp":
                fullf = os.path.join(dirpath,f)
                print "file=" + fullf 
                               
                #Process: Add Field ...
                try:
                    gp.AddField_management(fullf, "NewField", "DOUBLE", "10", "8", "", "", "NON_NULLABLE", "NON_REQUIRED", "")
                                 
                except:
                    print ("gp didn't work correctly")
                    
                    
                    #Add A Completion Message

                print ("Well done.")

    root = sys.argv [1]           ## or hard code the path to top level##
    os.path.walk(root,treeDir,None)
except:
    print ("Something went wrong.")


I would like to be able to set up arguments for field parameters as well, but first things first...

I have a second part to this question which involves Feature Class to Coverage, but I will start a second thread for that.  I will also be running that gp tool in the walk script.

Thanks in Advance.
Tags (2)
0 Kudos
8 Replies
MichaelVolz
Esteemed Contributor
Have you thought about trying

root = arcpy.GetParameterAsText(0)

where you are passing this as a value that the user can navigate to if you set it up as a directory in an ArcToolbox script?

This is how I am able to achieve what you want to do in your script.
0 Kudos
DebraGajdos
Deactivated User
I am sorry to say that I am not advanced enough to know how to set that up.
0 Kudos
MichaelVolz
Esteemed Contributor
Here is the link to setup a script tool where you can add a parameter to the script as as input:
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Adding_a_script_tool/00150000001r00000...

I followed this ESRI Help myself and I was able to setup a tool that would walk through all the subdirectories in a user selected directory.
0 Kudos
DebraGajdos
Deactivated User
Thanks.  I am currently using ArcGIS 9.2 with Python 2.4.  I know that things have changed in 9.3, version 10, and then with Python 3, so I will have to dig around a bit to make sure it works as you have shown.  I appreciate your help!
0 Kudos
ChrisSnyder
Honored Contributor
The code below verifies if the supplied path exists... Might help you figure out what's going wrong.

Curious that in Python/PythonWin 2.6.x , you can enter a path as an argument in all sorts of ways:
C:\Temp
C:\\Temp
C:/Temp
All these work... As I recall earlier versions of PythonWin didn't do this... I think they liked the 1st example. Could be wrong.

import os, sys
myPath = sys.argv[1]
if os.path.exists(myPath) == True:
   print "Yes Virginia, there is a " + str(myPath)
else:
   print "No, there is no such thing as " + str(myPath)
0 Kudos
DebraGajdos
Deactivated User
Yes, the flexablility has made me sloppy... 🙂

I do like your code to check the path - I can see that being helpful!

hmm, I tried this out and I got this error:

Traceback (most recent call last):
  File "C:/test/path_exists", line 3, in -toplevel-
    if os.path.exits(myPath) == True:
AttributeError: 'module' object has no attribute 'exits'

?
0 Kudos
markdenil
Frequent Contributor
Did you spell exists correctly?
it should be os.path.exists(myPath)

It is spelled os.path.exits(myPath) in the code block...
0 Kudos
ChrisSnyder
Honored Contributor
Woops - yes it is exists() not exits()... I fixed the code block above.
0 Kudos