Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am looking to move large number of mxds to a different location on the same drive. Many mxds haven't got 'Store relative pathways' option checked therefore this operation will result in broken links to data. I've only started using ArcGIS 10.1 and it is now possible to programmatically update relative pathways for all mxds by using arcPy and setting relativePaths to be "True". I had a go at writing Python script which (in theory) loops through a top folder and searches for all mxds and then updates "Store relative pathways" setting to be on. When I run this script in PyScripter, it fails to complete, I get the following error: "exceptions.NameError: name 'mxd' is not defined

Can anyone help ?!? Thanks in advance, Magda

    import arcpy, os

folderPath = r"C:\My Folder Path\...\...\..."

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)
            mxd.relativePaths = 'True'
del mxd
share|improve this question
Check if fullpath contain proper path by: print fullpath. Check code page for your strings (if all letters are encoded properly). –  Tomek Oct 22 '12 at 10:41
Also you may check if proper sythax is: mxd.relativePaths = 'True' or mxd.relativePaths = True . As 'True' is string and True is boolean. –  Tomek Oct 22 '12 at 12:38

2 Answers 2

Your code is good. I ran it on some example data I quickly created and the only problem I had was that you need to escape your backslashes in your file path on the second line:

folderPath = "C:\\My Folder Path\\...\\...\\...\\"

This should allow Python to get the paths to your mxd files and then change the setting. The other thing to note is that having spaces in file paths can cause problems, so avoid them if at all possible.

Hope this helps.

share|improve this answer
Hi SGrieve, Thanks for your answer. I doubled (I think that's what you mean by 'escape'?) the backslashes in my folder path and re-run the script. I got exactly the same error as before ('NameError: name 'mxd' is not defined). In the actual folder name there are no spaces so that shouldn't be an issue (I just typed it in). Any other suggestions please? –  Magda Oct 22 '12 at 10:11
I believe its either r'c:\' or 'c:\\' or 'c:/', not r'c:\\'. –  Tomek Oct 22 '12 at 10:19
As I said in my answer, with those alterations to the script it worked on my machine. Try indenting the line mxd.save() and re running the code. Also try inserting a print statement below the line 'if extension.lower() == ".mxd":' to see what value mxd is assigned, as it may be that the condition is never met. –  sgrieve Oct 22 '12 at 10:19
Tomek is correct, the r at the beginning of the string indicates a raw string that won't escape single backslashes. –  egdetti Oct 22 '12 at 13:32
My mistake, I've amended my answer. –  sgrieve Oct 22 '12 at 13:34

the error exceptions.NameError: name 'mxd' is not defined means that one of the if statements is preventing the code from getting to the line.

  mxd = arcpy.mapping.MapDocument(fullpath)

the solution is to determine which one is failing. The del mxd line is failing because mxd was never assigned a value because every time it went through the loop the If statement was never true.

In some cases it makes sense to set the variable to None before the loop, to prevent the program from getting an error at the del command. But in your case you want to investigate why the if statements are not correct.

share|improve this answer
That's great, thanks for all suggestions. I'll test it all out and report back.. –  Magda Oct 22 '12 at 14:50
Hi - just to update - I finally managed to get syntax to work and eliminated errors by adding 2 additional steps: 1. As suggested by mhoran_psprep, I set mxd variable to 'None' so it stopped complaining that mxd is not defined. 2. I've adjusted code from if extension.lower() == ".mxd" to be if filename.lower().endswith(".mxd"): and added mxdName = filename variable to the syntax. It then worked fine. Now the issue is version control (can't open 10.1 saved changes in 10.0) but 'save a copy' should work. Thanks for all comments and suggestions, much appreciated! Magda –  Magda Nov 1 '12 at 10:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.