help me with this script please.

624
4
01-19-2014 07:55 AM
Mostafa_Abd_El-Moniem_Abd_El-G
New Contributor
Hello group,
How can I Find all broken data sources in all map documents in a folder or a hard drive?

I'm trying to use this script but i always recieve an error

import arcpy.mapping as mapping, os
path = r"C:"
f = open('filename_here.txt','w')
for root,dirs,files in os.walk(path):
    for filename in files:
    basename, extension = os.path.splitext(filename)
        if extension == ".mxd":
            fullPath = os.path.join(path,filename)
            mxd = mapping.MapDocument(fullPath)
            f.write("MXD: " + filename + "\n")
            brknList = mapping.ListBrokenDataSources(mxd)
            for brknItem in brknList:
                f.write("\t" + brknItem.name + "\n")
f.close()

so please help me with this.
Thank you.
Tags (2)
0 Kudos
4 Replies
Luke_Pinner
MVP Regular Contributor
You need to tell us what the exact error is, it's very important. Copy the entire error message and post it here.

Also, please enclose your python code in [noparse]
[/noparse] tags to preserve the indentation. This is very important as indentation is is part of the syntax.  See this post for further information.
					
				
			
			
				
			
			
				
			
			
			
			
			
			
		
0 Kudos
Mostafa_Abd_El-Moniem_Abd_El-G
New Contributor
You need to tell us what the exact error is, it's very important. Copy the entire error message and post it here.

Also, please enclose your python code in [noparse]
[/noparse] tags to preserve the indentation. This is very important as indentation is is part of the syntax.  See this post for further information.



well the indentation is already preserved when i write the syntax.

when I set the path to drive(C)   "C:" which where the mxd files I don't get an error but the text file that was created is empty.
I tried to set the path to another drive i got an error but in this drive there isn't any mxd files.

this is the error mesage:

Traceback (most recent call last):
  File "C:\Python27\ArcGIS10.1\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript
    exec codeObject in __main__.__dict__
  File "G:\learn python\the book\ArcpyBook\Ch4\all.py", line 9, in <module>
    mxd = arcpy.mapping.MapDocument(fullPath)
  File "C:\Program Files\ArcGIS\Desktop10.1\arcpy\arcpy\arcobjects\mixins.py", line 608, in __init__
    assert (os.path.isfile(mxd) or (mxd.lower() == "current")), gp.getIDMessage(89004, "Invalid MXD filename")
AssertionError: Invalid MXD filename.
0 Kudos
BradPosthumus
Occasional Contributor II
The module os.walk lists all of the files within the "path" directory, including those within subdirectories of "path". In your case, this would include files not just under "C:" but also "C:\windows", "C:\users", etc. Which is what you expect.

However your code is listing just the filenames, checks to see if each one is an MXD, and if so then joins the mxd filename to "C:\". So if there's an MXD called "C:\users\map.mxd", your code is ultimately converting that path to "C:\map.mxd".

Fortunately os.walk also includes the full directory path of each file (named "root" in your code). Try changing "path" to "root" in this line:

fullPath = os.path.join(root,filename)


So the full code would look like this:

import arcpy.mapping as mapping, os
path = r"C:"
f = open('filename_here.txt','w')
for root,dirs,files in os.walk(path):
    for filename in files:
        basename, extension = os.path.splitext(filename)
        if extension == ".mxd":
            fullPath = os.path.join(root,filename)
            mxd = mapping.MapDocument(fullPath)
            f.write("MXD: " + filename + "\n")
            brknList = mapping.ListBrokenDataSources(mxd)
            for brknItem in brknList:
                f.write("\t" + brknItem.name + "\n")
f.close()
0 Kudos
Luke_Pinner
MVP Regular Contributor
well the indentation is already preserved when i write the syntax.


The indentation is not preserved when you submit your post.

Compare the code in your original post with the code below:

Original:
import arcpy.mapping as mapping, os
path = r"C:"
f = open('filename_here.txt','w')
for root,dirs,files in os.walk(path):
    for filename in files:
    basename, extension = os.path.splitext(filename)
        if extension == ".mxd":
            fullPath = os.path.join(path,filename)
            mxd = mapping.MapDocument(fullPath)
            f.write("MXD: " + filename + "\n")
            brknList = mapping.ListBrokenDataSources(mxd)
            for brknItem in brknList:
                f.write("\t" + brknItem.name + "\n")
f.close()

[noparse]With
[/noparse] tags:
import arcpy.mapping as mapping, os
path = r"C:"
f = open('filename_here.txt','w')
for root,dirs,files in os.walk(path):
    for filename in files:
    basename, extension = os.path.splitext(filename)
        if extension == ".mxd":
            fullPath = os.path.join(path,filename)
            mxd = mapping.MapDocument(fullPath)
            f.write("MXD: " + filename + "\n")
            brknList = mapping.ListBrokenDataSources(mxd)
            for brknItem in brknList:
                f.write("\t" + brknItem.name + "\n")
f.close()
0 Kudos