Select to view content in your preferred language

Python Script to Tool in Custom Toolbox

3324
13
01-31-2012 08:04 AM
BrittneyVenetucci
Emerging Contributor
Hi all,

I am trying to create a tool from a script that repairs all broken data sources in an mxd. I have posted the code as follows:
import arcpy, os
PYTHONPATH = "C:\Python26\ArcGIS10.0\lib\ntpath.pyc"
folderpath = arcpy.GetParameterAsText(0)
oldpath = arcpy.GetParameterAsText(1)
newpath = arcpy.GetParameterAsText(2)
print 'running'

i=0
#create a folder within a local folder to store the mxds
folderPath = r""

for filename in os.listdir(folderPath):
   
    fullpath = os.path.join(folderPath, filename)
   
    if os.path.isfile(fullpath):
       
        basename, extension = os.path.splitext(fullpath)
       
        if extension.lower() == ".mxd":
           
            mxd = arcpy.mapping.MapDocument(fullpath)
           
            oldpath = ""
            newpath = r""
            #mxd.replaceWorkspaces(oldpath, "SDE_WORKSPACE", newpath, "SDE_WORKSPACE",True)
            print "ok"
            mxd.findAndReplaceWorkspacePaths(oldpath, newpath)
            #mxd.save()
            mxd.save()
      
           
        print '...', basename, extension, "finished"

However, when I run the tool it does not work. Any ideas as to what I have to set my input parameters as or if there is something wrong in my code?

Thanks!

Brittney
Tags (2)
0 Kudos
13 Replies
BenjaminGale
Occasional Contributor
From the code you posted the only thing that I really notice is that it appears that you are changing your input variables before you use them.
folderPath = r""
oldpath = ""
newpath = r""

I took those out and changed the extention.lower() to extention.endswith (looked like you were checking if the file was an mxd?). I think it should work now.


import arcpy, os

#Parameters (workspaces)
folderpath = arcpy.GetParameterAsText(0)
oldpath = arcpy.GetParameterAsText(1)
newpath = arcpy.GetParameterAsText(2)

#Loop through folder
for filename in os.listdir(folderPath):
  fullpath = os.path.join(folderPath, filename)
  if os.path.isfile(fullpath):
    basename, extension = os.path.splitext(fullpath)
    if extention.endswith("mxd"):
         mxd = arcpy.mapping.MapDocument(fullpath)
         mxd.findAndReplaceWorkspacePaths(oldpath, newpath)
         mxd.save() 



If not...
It might be easier to test the basic workflow then add in your loop.

You could try making a tool out of this...it will only do one mxd at a time but it should work. If you leave the input for oldPath empty it should update the workspaces for all the layers to the new path.

import arcpy

#Inputs
mxdIn = arcpy.GetParameterAsText(0) #ArcMapDocument
oldPath = arcpy.GetParameterAsText(1) #Workspace
newPath = arcpy.GetParameterAsText(2) #Workspace

#Set mxd
mxd = arcpy.mapping.MapDocument(mxdIn)

#Change workspace paths of layers
mxd.findAndReplaceWorkspacePaths(oldPath, newPath)

#Save mxd (overwrites mxd)
mxd.save()
0 Kudos
BrittneyVenetucci
Emerging Contributor
PYTHONPATH is a Windows (or UNIX) environment variable that needs to be set before the script is run, if you need it. The purpose of that variable is to modify sys.path when Python launches. Sys.path is a list of folders that tells Python where to look for imports, ie "import mymodule" will look for a file "mymodule.py" or "mymodule.pyc" through the sys.path, and use the first one it finds.

It looks like you are trying to invoke the ntpath module. This module is already in the sys.path, so you could get to it with:

import ntpath


However, you don't need to import it -- the os module is what you want (and what you are using).  I'm pretty sure os calls ntpath for you on windows, something else on Mac or Unix.

If you need to alter where to look at runtime, you can alter the sys.path as follows.

import sys
sys.path.append(r"C:\mytools\python\modules")


You can check out where imports will look by listing your sys.path:
>>> import sys
>>> sys.path


Note, by default, "" is the first item in sys.path. So if you include a custom module in the same folder as the script, it will always use that version.

http://docs.python.org/tutorial/modules.html


I attempted to run both scripts and they both failed. The first script said folderPath is not defined and the second secript said that there was an invalid mxdname. Any ideas?

For the creation of the tool, are their any output variables that should be created?
0 Kudos
BrittneyVenetucci
Emerging Contributor
I attempted to run both scripts and they both failed. The first script said folderPath is not defined and the second secript said that there was an invalid mxdname. Any ideas?

For the creation of the tool, are their any output variables that should be created?
0 Kudos
BenjaminGale
Occasional Contributor
Strange the second should have worked.
I made it into a tool and tested it on a mxd that I broke the data sources in and it appeared to work.

I'll put the tool I made below in case it will work for you. Just extract the toolbox and script into the same folder and specify an mxd and new path. Leaving the old path blank should update all the layers.

If it still doesn't work there may be a step I am over looking or am not aware of.
What is the scenario for the new paths compared to the old ones? Is the data for the layers arranged or named differently?

EDIT:
I should note running the tool with the target mxd open appears to crash ArcMap.
0 Kudos