Thanks for the prompt responses everyone! I can always count on you.
Credit for the solution goes to Wai Ming Chan and Jim Herberich:
In a rather surprising Friday afternoon twist, it was not in the spaces in the filenames. I had this code running last week and accidentally saved over it. I encountered the error while trying to reconstruct
it. Previously the spaces in the filenames had not given me any issues, so I was a little skeptical.
The problem was in the fullPath variable.
It was: fullPath = os.path.join(path, fileName)
It should have been: fullPath = os.path.join(root, fileName)
Thanks again to everyone who weighed in. I’ve asked my fellow staff to also refrain from naming files with spaces in them. ;)
Happy Friday!
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.
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.