Suggest using the forward slash instead of the backslash, i.e. instead of path = r"I:\ARC\DATA\MXDs" use path = r"I:/ARC/DATA/MXDs"

 

Python should interpret that correctly when it comes to file names.   Backslash is an escape character.   If you want to use backslash it needs to be repeated, like this:  path = r"I:\\ARC\\DATA\\MXDs"

 

-Tony Schaffer

 City of Manchester, NH Information Systems Department

 100 Merrimack Street,  Manchester, NH 03101

 Phone: (603) 624-6519 x2309

 Email: [log in to unmask] 

 

From: Northeast Arc Users Group [mailto:[log in to unmask]] On Behalf Of Lachance, Michael - APHIS
Sent: Friday, June 06, 2014 1:38 PM
To: [log in to unmask]
Subject: Python: Invalid MXD filename

 

Would anyone mind taking a look at this Python code and telling me why it says my MXD filename is invalid?

 

import arcpy, os

 

path = r"I:\ARC\DATA\MXDs"

 

for root, dirs, files in os.walk(path):

    for fileName in files:

        fullPath = os.path.join(path, fileName)

        basename, extension = os.path.splitext(fullPath)

        mxd = arcpy.mapping.MapDocument(fullPath)

        if extension == ".mxd":           

            for df in arcpy.mapping.ListDataFrames(mxd):

                for lyr in arcpy.mapping.ListLayers(mxd,"",df):

                    findthis = value1

                    findthat = value2

                    if findthis in lyr.name or findthat in lyr.name:

                        lyr.replaceDataSource(r'I:/ARC/Data/ITrees.gdb','FILEGDB_WORKSPACE','Trees')

                        print(lyr.name + " - Layer Resourced")

                    else:

                        pass

                        print lyr.name + ": Pass"

                    arcpy.RefreshTOC()

                    arcpy.RefreshActiveView()

                    mxd.save()

print "--Script complete--"

---------------------------------------------------------------------------------------------------

Results:

 

Traceback (most recent call last):

  File "I:\ARC\Data\Expressions\Trees_Walk.py", line 9, in <module>

    mxd = arcpy.mapping.MapDocument(fullPath)

  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\arcobjects\mixins.py", line 609, in __init__

    assert (os.path.isfile(mxd) or (mxd.lower() == "current")), gp.getIDMessage(89004, "Invalid MXD filename")

AssertionError: Invalid MXD filename.

 

 

 

When I print the variable “fullPath” it looks something like this:

 

I:\ARC\DATA\MXDs\Clinton Base Map.mxd

 

But when I feed the value  into

 

mxd = arcpy.mapping.MapDocument(fullPath)

 

it doesn’t like it anymore

 

 

Kind regards,

 

Michael Lachance

Plant Protection Technician

ALB Eradication Program USDA APHIS

151 West Boylston Drive

Worcester, MA 01606

508.852.8050 (o)

508.414.5673 (c)

The USDA is an equal opportunity provider and employer.

Federal Relay Service (Voice/TTY/ASCII/Spanish) 1-800-877-8339

 

This electronic message contains information generated by the USDA solely for the intended recipients. Any unauthorized interception of this message or the use or disclosure of the information it contains may violate the law and subject the violator to civil or criminal penalties. If you believe you have received this message in error, please notify the sender and delete the email immediately.

 

------------------------------------------------------------------------- This list (NEARC-L) is an unmoderated discussion list for all NEARC Users.

If you no longer wish to receive e-mail from this list, you can remove yourself by going to http://listserv.uconn.edu/nearc-l.html.




The Right-To-Know Law (RSA 91-A) provides that most e-mail communications, to or from City employees and City volunteers regarding the business of the City of Manchester, are government records available to the public upon request. Therefore, this email communication may be subject to public disclosure.
------------------------------------------------------------------------- This list (NEARC-L) is an unmoderated discussion list for all NEARC Users.

If you no longer wish to receive e-mail from this list, you can remove yourself by going to http://listserv.uconn.edu/nearc-l.html.