workspace as network drive

2410
21
Jump to solution
07-30-2013 12:57 PM
AmyKlug
Occasional Contributor III
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
1 Solution

Accepted Solutions
RhettZufelt
MVP Frequent 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.

View solution in original post

0 Kudos
21 Replies
RhettZufelt
MVP Frequent Contributor
Sure, I have done it a couple ways:

Assuming the networked computer is named "server1" and has a share named "U" and/or is mapped to drive U:

import arcpy

workspace = arcpy.env.workspace = r"U:"    ## don't put the slash at the end.....
workspace = arcpy.env.workspace = r"\\server1\U"





either works.

Keep in mind, though, setting it to the base level, by default, will drill down into all directories in there as well, so it can take a long time if there is much there.  Better to set it to a working folder on the drive.  Mine is still running as it is categorizing my entire drive.

R_


When topdown is True, the dirnames list can be modified in-place, and Walk() will only recurse into the subworkspaces whose names remain in dirnames. This can be used to limit the search, impose a specific order of visiting, or even to inform Walk() about directories the caller creates or renames before it resumes Walk() again. Modifying dirnames when topdown is False is ineffective, because in bottom-up mode the workspaces in dirnames are generated before dirpath itself is generated.

0 Kudos
JamesGirvan
New Contributor
I am interested in doing something similar.  However, my reference needs to be to a SQL Enterprise GDB feature class within a feature dataset.  The server is somcty06, FDS is named Cadastre and the FC is Parcels.  I am trying to create a parcel search tool available to all of my users that will require entering a municipal name from a drop down list then freely entering text for the block and lot numbers.  The tool should then zoom to the selected parcel or return No Parcel Found message.
0 Kudos
RhettZufelt
MVP Frequent Contributor
James,

You would go into ArcCatalog, Database Connections, Add Database Connection and set it up to your database.

Then you can reference as such:

indatabase = "Database Connections\\Connection to RCES_P.sde\\SIS_ADM.IMAGES"  # can use the database connection

indatabase = "C:\\Users\\rkzufelt\\AppData\\Roaming\\ESRI\\Desktop10.1\\ArcCatalog\\Connection to RCES_P.sde\\ARCUPDATE.MAPS"  #or the database connection file





R_
0 Kudos
AmyKlug
Occasional Contributor III
Sure, I have done it a couple ways:

Assuming the networked computer is named "server1" and has a share named "U" and/or is mapped to drive U:

import arcpy

workspace = arcpy.env.workspace = r"U:"    ## don't put the slash at the end.....
workspace = arcpy.env.workspace = r"\\server1\U"





either works.

Keep in mind, though, setting it to the base level, by default, will drill down into all directories in there as well, so it can take a long time if there is much there.  Better to set it to a working folder on the drive.  Mine is still running as it is categorizing my entire drive.

R_


Thanks, Yes..Unfortunaltey I will have to drill down into all directories.

Another question..... I have been trying to write my results to a geodatabase table and keep getting an error that the size of the cell and path form my list aren't the same size. I have had sucess writing to csv.....but I'm pretty sure there should be a way to write to GDB table?
0 Kudos
RhettZufelt
MVP Frequent Contributor
To write to the FGDB table you can do it several ways.  If you are modifying data in an existing row, you could use updateCursor, or calculatefield.
If you are adding new rows to the table, you want InsertCursor
http://resources.arcgis.com/en/help/main/10.1/index.html#//018w00000014000000
http://resources.arcgis.com/en/help/main/10.1/index.html#//00170000004m000000

R_

Also, keep in mind that the da.walk with topdown set as true, you can remove any of the directories from the walk list.  you say you need to drill down, but if you don't need to drill into ALL subdirs, this can be done with da.walk.

The bottom example here http://resources.arcgis.com/en/help/main/10.1/index.html#//018w00000023000000 shows that with the dirnames.remove statement.
0 Kudos
AmyKlug
Occasional Contributor III
Forgot to tell you I am using the insert cursor method and was getting this message:

"TypeError: sequence size must match size of the row"

I just got it to work, I needed to use arcpy.Describe before appending to my list

>>> import arcpy
>>> import os
>>> workspace = r"H:/LIBTest"
>>> newtable = r"H:/TableList.gdb/GISLibrary"
>>> list = []
>>> for dirpath, dirnames, filenames in arcpy.da.Walk(workspace):
...     for filename in filenames:
...         descr = arcpy.Describe(os.path.join(dirpath, filename))
...         list.append([descr.catalogPath, descr.name])
...         
>>> with arcpy.da.InsertCursor(newtable, ['Path', 'FileName']) as insert:
...     for f in list:
...         insert.insertRow(f)
... 
0 Kudos
AmyKlug
Occasional Contributor III
So I keep getting errors that certain tools in a toolbox don't exist. I can't get past the first one which is the "con" tool in the spatial analyst toolbox so I made sure I had spatial analyst extension checked to no avail. Doesn't seem like that should make a difference?
0 Kudos
RhettZufelt
MVP Frequent Contributor
No, you would just get a not licesed error if that were the case.

If you have too much security, you will get unable to run errors as they rely on ActiveX being enabled now.

If it is not finding it, even after a re-boot of the computer, you may need a re-install.

Does it not find them in ArcMap or ArcCatalog?

That is a weird one, as if it is showing you the list under Conditional, then it has to be seeing the Spatial Analyst Tools.tbx...

R_
0 Kudos
AmyKlug
Occasional Contributor III
I just ran the tool on my C drive and it's telling me another file does not exist (a table in a access .mdb). Is there a way to write in the code for it to continue on if it can't find the file?

EDIT: The access table was linked and does not exist in the parent database

I am going to try running in ArcCatalog next. The folder the code is failing on is an archive folder and contains toolboxes from Arc 9.3 but "con" exists in both versions so I don't know. I would still like to skip those errors, not sure if try/except would work?
0 Kudos