0

I have a number of .mxd files in a folder, in which i need to run certain operations. On one of them, specifically, when I try assigning it to a variable, I keep getting this error (and it's not the first of the lot):

Traceback (most recent call last):
  File "<pyshell#29>", line 1, in <module>
    mxd = arcpy.mapping.MapDocument(mxd_path)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\arcobjects\mixins.py", line 611, in __init__
    assert (os.path.isfile(mxd) or (mxd.lower() == "current")), gp.getIDMessage(89004, "Invalid MXD filename")
AssertionError: Invalid MXD filename.

However, I'm pretty sure the name of the file is correct, and I can normally open it in ArcMap. Would anyone know what could be causing it? Here is the code I've been using to do it (fully functional for the others...):

import arcpy
import os
from os.path import join
mxdPath = "C://1331/DB/Original Files/MXD/"
for root, dirs, filenames in os.walk(mxdPath):
        for f in filenames:
        mxd_path = os.path.join(root, f)
        mxd = arcpy.mapping.MapDocument(mxd_path) #This line returns the error for one specific file

        #Some processing on the file: changing references, saving it as new file, etc;
        del mxd

If it helps, the problematic file is called: "Figura 2.11 - Áreas Prioritárias.mxd". I've tried changing its name (other files have spaces and special characters, but the code runs without an issue for them), but it didn't help either. All the other files in the folder were made by the same person and in the same version of ArcGis, and are named alike. Eg: "Figura 2.10 - Unidades de Conservação", "Figura 2.13 - Densidade Demográfica", etc. The code works for these without an issue.

11
  • Welcome to GIS SE! As a new user please take the tour to learn about our focused Q&A format. Can you give some examples of other filenames that contain spaces and/or special characters? Are they the same special characters? Please edit your question to include this additional info.
    – Midavalo
    Commented Feb 21, 2017 at 21:19
  • 1
    If you copy your file to something like C:\temp\test.mxd does the same code work on that?
    – PolyGeo
    Commented Feb 21, 2017 at 21:20
  • Thanks for the suggestion! Copying it to "C://temp/" and erasing all special characters and spaces did the trick. I would like to do it with the original names, but it's not a big issue. I still don't understand though why it works for some, but not for all (length of the full filepath is not the problem either, since this is not the largest). Commented Feb 21, 2017 at 21:33
  • 1
    You need the "raw" modifier on string constants with path backslashes. Creating files with spaces in their names is a disaster waiting to happen.
    – Vince
    Commented Feb 21, 2017 at 21:40
  • 1
    You could potentially rename/copy the file first, process your mxd, and then rename/copy it back to keep the filename. That said I think removing spaces and special characters is preferred when using python.
    – Midavalo
    Commented Feb 21, 2017 at 21:43

2 Answers 2

0

Try setting your mxdPath as unicode by adding a u before the string

mxdPath = u"C:\\1331\\DB\\Original Files\\MXD"

Also leave out the trailing slash \ as it's not needed for os.path.join() to work, and has the potential to cause issues.

import arcpy, os

mxdPath = u"C:\\1331\\DB\\Original Files\\MXD"
for root, dirs, filenames in os.walk(mxdPath):
        for f in filenames:
        mxd_path = os.path.join(root, f)
        mxd = arcpy.mapping.MapDocument(mxd_path) #This line returns the error for one specific file

        #Some processing on the file: changing references, saving it as new file, etc;
        del mxd
0

Using r before the path to indicate raw may solve the problem:

mxdPath = r"C://1331/DB/Original Files/MXD/"

Maybe even remove your extra /:

mxdPath = r"C:/1331/DB/Original Files/MXD"

Or do what ERSI suggests. raw format and single backslash \:

mxdPath = r"C:\1331\DB\Original Files\MXD"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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