workspace as network drive

3934
21
Jump to solution
07-30-2013 12:57 PM
AmyKlug
Frequent Contributor
Is it possible to set the the workspace to a network drive and if so how would you write it?

When using arcpy.da.walk and workspace = "U:/" ,  it froze up my computer...or it was just really slow.
Tags (2)
0 Kudos
21 Replies
RhettZufelt
MVP Notable Contributor
First, are you running the toolbox tool, or running code?

If coding, there is normally a way to tell it to move on if it can't find the file.  However, since you have to tell it what file(s) to use, normally, you just don't pass a non-existent file to it.

also, not sure why you are getting that error as the only valid inputs for the con tool are rasters, so it should never be looking for a table, yet alone in an mdb.

R_
0 Kudos
AmyKlug
Frequent Contributor
I am creating a table of filenames, paths and data type using arcpy.da.Walk in a script tool. When it gets to the "con" tool it says it can't find it (even though it's there and usable) and the code quits. I just tried it in ArcCatalog and same result. Then copied the "con" tool from the 10.1 system toolbox into another toolbox, ran the script...... it failed. When I deleted the "con" tool from the toolbox, the script worked fine. I tested it with some other spatial analyst tools and had no problems. I would really like to have my code skip the data it can't find.

arcpy.da.Walk thinks the MDB's are persoanl GDB's I guess. I edited my post above that the table it could not find was linked and the tool could not find it because it had been deleted from the original database.
0 Kudos
RhettZufelt
MVP Notable Contributor
Could put something like this in there:

if not arcpy.Exists(filename):
     continue


this way, if the filename doesn't exist, it will move on to the next iteration.

also, keep in mind that when using da.walk, the actuall path to and including the filename is

os.path.join(dirpath,filename)   not just "filename", especially important if the file is not in your set env.workspace.

there are some posts here that also show how to code for ( if not in something, then do something) http://forums.arcgis.com/threads/89638-List-features-in-multiple-workspaces-then-convert-to-point-BU...  that might come in handy.

as far as the script tool issues, can't help you there, I do all my stuff stand alone.

R_
0 Kudos
AmyKlug
Frequent Contributor
Code gets hung up using stand-alone too, it just doesn't recognize the "con" tool for some reason. I will try .Exists
0 Kudos
RhettZufelt
MVP Notable Contributor
Like I said, I don't make script tools, but I do use con now and then, I just use arcpy for it.

http://resources.arcgis.com/en/help/main/10.1/index.html#//009z00000005000000

R_
0 Kudos
AmyKlug
Frequent Contributor
Your suggestion to use .Exist worked perfectly.........

Still very curious why it errored that "con" does not exist, we may never know



The almost there product:

import arcpy
import os
workspace = r"U:"
newtable = r"H:/TableList.gdb/GISLibrary"
list = []
for dirpath, dirnames, filenames in arcpy.da.Walk(workspace):
    if not arcpy.Exists(filenames):
        continue
        for filename in filenames:
            descr = arcpy.Describe(os.path.join(dirpath, filename))
            list.append([descr.catalogPath, descr.name, descr.dataType])
with arcpy.da.InsertCursor(newtable, ['Path', 'FileName', 'Type']) as insert:
    for f in list:
        insert.insertRow(f)
0 Kudos
AmyKlug
Frequent Contributor
Your suggestion to use .Exist worked perfectly.........

Still very curious why it errored that "con" does not exist, we may never know



The final product:

import arcpy
import os
workspace = r"U:"
newtable = r"H:/TableList.gdb/GISLibrary"
list = []
for dirpath, dirnames, filenames in arcpy.da.Walk(workspace):
    if not arcpy.Exists(filenames):
        continue
        for filename in filenames:
            descr = arcpy.Describe(os.path.join(dirpath, filename))
            list.append([descr.catalogPath, descr.name, descr.dataType])
with arcpy.da.InsertCursor(newtable, ['Path', 'FileName', 'Type']) as insert:
    for f in list:
        insert.insertRow(f)


The code above actually did not work the next day....but this should be correct (although this code is now saying all tools don't exist, might be I need to use something different than arcpy.Exists(filename) or how I defined the environment/workspace or setting my parameter as a workspace but still working that out) :

import arcpy
import os
p = arcpy.GetParameterAsText(0)
arcpy.env.workspace = p
newtable = r"H:/TableList.gdb/GISLibrary"
list = []
for dirpath, dirnames, filenames in arcpy.da.Walk(p):
    for filename in filenames:
        if arcpy.Exists(filename):
            arcpy.AddMessage("adding file to list")
            descr = arcpy.Describe(os.path.join(dirpath, filename))
            list.append([descr.catalogPath, descr.name, descr.dataType])
        else:
           arcpy.AddMessage('File   ' + filename + 'does not exist')
        continue 
with arcpy.da.InsertCursor(newtable, ['Path', 'FileName', 'Type']) as insert:
    for f in list:
        insert.insertRow(f)


EDIT: this code only works when i chose a geodatabase as input p. if I used a folder (that contained geodatabases) as input all of my feature classes were tagged as "do not exist)
0 Kudos
RhettZufelt
MVP Notable Contributor
import arcpy import os p = arcpy.GetParameterAsText(0) arcpy.env.workspace = p newtable = r"H:/TableList.gdb/GISLibrary" list = [] for dirpath, dirnames, filenames in arcpy.da.Walk(p):     for filename in filenames:         if arcpy.Exists(os.path.join(dirpath, filename)):             arcpy.AddMessage("adding file to list")             descr = arcpy.Describe(os.path.join(dirpath, filename))             list.append([descr.catalogPath, descr.name, descr.dataType])         else:            arcpy.AddMessage('File   ' + filename + 'does not exist')         continue  with arcpy.da.InsertCursor(newtable, ['Path', 'FileName', 'Type']) as insert:     for f in list:         insert.insertRow(f)


EDIT: this code only works when i chose a geodatabase as input p. if I used a folder (that contained geodatabases) as input all of my feature classes were tagged as "do not exist)


Suspect the p variable is not getting passed correctly as the da.walk works fine for FGDB or the folder. Especially since the way you have it, it will only evaluate to False if you end up with an empty filenames list, which means that either p is the wrong path, or there is NODATA in there at all. Basically, da.walk makes a list, then you iterate through it and say "This file exists, so I put it in my list, if this file doesn't exist, then: so, the only way this would not be true is if the list is empty, so deleting that line here would have the same effect (minus the error of course on empty list). Also, the continue statement you have is not needed. Basically, it is the last line in the for loop. So, you are telling it, that when it is finished will all the code for the first item in the list, continue to the next. this is the default behaviour. Don't think it would make a difference, but possibly telling it to skip one?

perhaps you need to set p = to:
p = str(arcpy.GetParameterAsText(0)) or something. Like i said, don't tool script tools, so not sure how a single parameter gets passed.

can print p
and type(p) to make sure it is a string and the value is that actual path/name to the folder you want to walk through.

The code above actually did not work the next day....but this should be correct (although this code is now saying all tools don't exist, might be I need to use something different than arcpy.Exists(filename) or how I defined the environment/workspace or setting my parameter as a workspace but still working that out)


for arcpy.Exists:
 Tests for the existence of feature classes, tables, datasets, shapefiles, workspaces, layers, and files in the current workspace


Don't think it was intended to look for "Tools", of course, a toolbox is a .tbx "file" so it should find them. However, I'm a little confused here. I don't do script tools, but I know that you have to tell the script what tool to use, where the toolbox is, etc. and I see no reference what so ever in your script to any tools or toolboxes. where are you defining them, and where are you calling the con tool that is giving you the error?

The spatial analyst toolbox should be here:
c:\program files\arcgis\desktop10.1\ArcToolbox\Toolboxes\Spatial Analyst Tools.tbx

R_

Shoot, just saw something else going on here. Try the change in red above. you are checking to see if a file exists, da.walk reports filenames and directories sepratly, so you need to pass the path and name to locate the file. Just like in your describe statement.
0 Kudos
AmyKlug
Frequent Contributor
I tried using:

if arcpy.Exists(os.path.join(dirpath, filename)):



In an earlier version before the code was this far along. But this REALLY did the trick. It only errors the "con" tool now (very strange). Using a parameter with a script tool I can just select the network drive.....might have to do more than one.
Thanks for all your help!

Here it is:

import arcpy
import os
p = arcpy.GetParameterAsText(0)
arcpy.env.workspace = p
newtable = r"H:/TableList.gdb/GISLibrary"
list = []
for dirpath, dirnames, filenames in arcpy.da.Walk(p):
    for filename in filenames:
        if arcpy.Exists(os.path.join(dirpath, filename)):
            arcpy.AddMessage('adding ' + os.path.join(dirpath, filename)  + ' to list............')
            descr = arcpy.Describe(os.path.join(dirpath, filename))
            list.append([descr.catalogPath, descr.name, descr.dataType])
        else:
           arcpy.AddMessage('ERROR: ' + os.path.join(dirpath, filename) + ' does not exist')
        continue 
with arcpy.da.InsertCursor(newtable, ['Path', 'FileName', 'Type']) as insert:
    for f in list:
        insert.insertRow(f)
0 Kudos
AmyKlug
Frequent Contributor

Don't think it was intended to look for "Tools", of course, a toolbox is a .tbx "file" so it should find them.  However, I'm a little confused here.  I don't do script tools, but I know that you have to tell the script what tool to use, where the toolbox is, etc. and I see no reference what so ever in your script to any tools or toolboxes.  where are you defining them, and where are you calling the con tool that is giving you the error?


someone had made a copy of another server and put it on the U drive of the server I am cataloging, so we have a toolbox in the U(current server):/otherserver/......../program files/arcgis/........./toolbox. arcpy.da.walk does not recognize that tool for some reason. I tried the code on other toolboxes and same result. What is happening is that it is not writing the CON tool to my library table like it does with the other tools, i am not trying to use the con tool.
0 Kudos