Select to view content in your preferred language

Find and Replace Workspace Path not working

5848
10
06-21-2012 08:13 AM
MichaelMiller2
Frequent Contributor
Attempting to use the find and replace workspace path, but it's returning an error message. Any suggestions on why it's not working?

import arcpy
import os

ckFolder = r"\\itdhq1apt50\gis_data\testUpdatePaths\FremontCounty"
arcpy.env.workspace = ckFolder
print "Checking Folder: " + ckFolder
arcpy.AddMessage("Checking Folder: " + ckFolder)

for mDoc in arcpy.ListFiles("*.mxd"):
    mxd = os.path.join(ckFolder, mDoc)
    #print fullpath
    #mxd = arcpy.mapping.MapDocument(ckFolder + os.sep + mDoc)
    arcpy.AddMessage("MXD Name: " + mxd)
    mxd.findAndReplaceWorkspacePaths(r"J:\GIS\JHill\SDE_To_Route", r"\\itdgishq\hqgis\LocalRds\SDE_To_Route", "FALSE")
    mxd.save()



Traceback (most recent call last):
  File "C:\Python26\ArcGIS10.0\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 312, in RunScript
    exec codeObject in __main__.__dict__
  File "\\Itdgishq\hqgis\updateDataPaths.py", line 25, in <module>
    mxd.findAndReplaceWorkspacePaths(r"J:\GIS\JHill\SDE_To_Route", r"\\itdgishq\hqgis\LocalRds\SDE_To_Route", "FALSE")
AttributeError: 'unicode' object has no attribute 'findAndReplaceWorkspacePaths'
Tags (2)
0 Kudos
10 Replies
DarrenWiens2
MVP Alum
Why did you comment out the line: #mxd = arcpy.mapping.MapDocument(ckFolder + os.sep + mDoc)?

You need a MapDocument to use findAndReplaceWorkspacePaths. The value returned from os.path.join isn't a MapDocument, it's a path to a MapDocument.
0 Kudos
MichaelMiller2
Frequent Contributor
Thanks Darren for pointing that out.

I've revised the script as follows, and now it crashes PythonWin. I know I'm overlooking something, just not sure what it is.

import arcpy
import os

#ckFolder = arcpy.GetParameterAsText(0)
ckFolder = r"\\itdhq1apt50\gis_data\testUpdatePaths\FremontCounty"
arcpy.env.workspace = ckFolder
print "Checking Folder: " + ckFolder
arcpy.AddMessage("Checking Folder: " + ckFolder)

for mDoc in arcpy.ListFiles("*.mxd"):
    fullPath = os.path.join(ckFolder, mDoc)
    mxd = arcpy.mapping.MapDocument(fullPath)
    print fullPath
    #print mxd
    mxd.findAndReplaceWorkspacePaths(r"J:\GIS\JHill\SDE_To_Route", r"\\itdgishq\hqgis\LocalRds\SDE_To_Route", "TRUE")
    mxd.save()




Why did you comment out the line: #mxd = arcpy.mapping.MapDocument(ckFolder + os.sep + mDoc)?

You need a MapDocument to use findAndReplaceWorkspacePaths. The value returned from os.path.join isn't a MapDocument, it's a path to a MapDocument.
0 Kudos
DarrenWiens2
MVP Alum
I don't know if this will help, but you should delete your mxd when you're done with it: del mxd
0 Kudos
MichaelMiller2
Frequent Contributor
Using del mxd at the end of each loop didn't change anything.....still crashing PythonWin.


I don't know if this will help, but you should delete your mxd when you're done with it: del mxd
0 Kudos
JeffBarrette
Esri Regular Contributor
One possible issue is your "TRUE" statement in:

mxd.findAndReplaceWorkspacePaths(r"J:\GIS\JHill\SDE_To_Route", r"\\itdgishq\hqgis\LocalRds\SDE_To_Route", "TRUE")


Try:

mxd.findAndReplaceWorkspacePaths(r"J:\GIS\JHill\SDE_To_Route", r"\\itdgishq\hqgis\LocalRds\SDE_To_Route", True)


or because True is the default value, try:

mxd.findAndReplaceWorkspacePaths(r"J:\GIS\JHill\SDE_To_Route", r"\\itdgishq\hqgis\LocalRds\SDE_To_Route")


Jeff
0 Kudos
MichaelMiller2
Frequent Contributor
Aparently there is a bug using this method in ArcGIS 10.0.  I'm in the process of re-working a script to accomplish this same feat using the layer class instead.  Any suggestions or guidance is appreciated.


One possible issue is your "TRUE" statement in:

mxd.findAndReplaceWorkspacePaths(r"J:\GIS\JHill\SDE_To_Route", r"\\itdgishq\hqgis\LocalRds\SDE_To_Route", "TRUE")


Try:

mxd.findAndReplaceWorkspacePaths(r"J:\GIS\JHill\SDE_To_Route", r"\\itdgishq\hqgis\LocalRds\SDE_To_Route", True)


or because True is the default value, try:

mxd.findAndReplaceWorkspacePaths(r"J:\GIS\JHill\SDE_To_Route", r"\\itdgishq\hqgis\LocalRds\SDE_To_Route")


Jeff
0 Kudos
JoeFlannery
Honored Contributor
I, too, am having no success running this code in ArcMap 10.0, SP4.  I am using code examples found in ArcGIS Online Help and running it against a very simple map and data sets.  Python crashes every time.  I have placed "print" statements in my code to see how far it goes and it gets to the findAndReplaceWorkspacePaths process and crashes.  I've run out of ideas.

import arcpy, os

# Local variables
oldSDE = r"M:\Clients\M-O\ABC\_ArcGIS\2012\06\TestingRepath\SQL01\\"
newSDE = r"M:\Clients\M-O\ABC\_ArcGIS\2012\06\TestingRepath\SQL02\\"
mapDoc = r"M:\Clients\M-O\ABC\_ArcGIS\2012\06\TestingRepath\_Map01.mxd"

print "Define MapDocument Parameter"
mxd = arcpy.mapping.MapDocument(mapDoc)

print "Start FindAndReplace Process"
mxd.findAndReplaceWorkspacePaths(oldSDE, newSDE)

print "Start saveAcopy Process"
mxd.saveACopy(r"M:\Clients\M-O\ABC\_ArcGIS\2012\06\TestingRepath\_Map01_NEW.mxd")

print "Done"

del arcpy, oldSDE, newSDE, mapDoc, mxd
0 Kudos
JoeFlannery
Honored Contributor
I found this forum posting:

http://forums.arcgis.com/threads/55556-findAndReplaceWorkspacePaths-is-crashing-Python-script?highli...

I had to remove the Esri and Bing map background services from my ArcMap table-of-contents to make my findAndReplaceWorkspacePaths python script work.  And, as the above mentioned forum post notes, my SDE paths were not changed in the output saveACopy MXD.  So, the script did not crash with online map background services removed, but it also did not make any path changes.

Almost every map document that we create has an Esri/Bing map background service.  If Mapdocument.findAndReplaceWorkspacePaths does not work on an MXD containing online services, what are my options?
0 Kudos
JeffBarrette
Esri Regular Contributor
The Bing maps services issue is fixed with 10.1 SP1  (NIM081549).

Jeff
0 Kudos