Hi,
I'm attempting to print lists of broken source data to a text file using the write to text function, but I'm at a loss. The code I have works up until it's meant to write to the text file.
You'll notice that the write function has nothing between the parentheses. This is because I'm not sure what to put there in order to print the list of broken layers and broken picture elements. In other words, I'm uncertain as to what variable to use for the write function. Basically, I'm trying to write the lists shown in the Interactive Window below (PrtScn) to a text file, and save the file in the workspace folder.
import arcpy
import os
from arcpy import env
# set variables
path = r'\\gisfile\GISmaps\AtlasMaps\ATLAS_MAPS_17\New folder'
# iterates through folder and lists broken layers
for fileName in os.listdir(path):
fullPath = os.path.join(path,fileName)
if os.path.isfile(fullPath):
basename,extension = os.path.splitext(fullPath)
if extension == ".mxd":
mxd = arcpy.mapping.MapDocument(fullPath)
print "current MXD being checked is: " + fileName
for brknList in arcpy.mapping.ListBrokenDataSources(mxd):
print "\t broken layer: " + brknList.name
# iterates through folder and checks for broken picture element source
for elem in arcpy.mapping.ListLayoutElements(mxd, "PICTURE_ELEMENT", "*logo*"):
print "\t broken picture element name: " + elem.name
# Write to text file
txtFile = open(path + "{}".format("BrkSrceLst.txt"), "w")
txtFile.write("MXDs that have broken source data" + "\n")
txtFile.write("----------------------------------------------" + "\n")
for f in fullPath:
f.write()
for ename in elem.name:
ename.write()
txtFile.close()
print "\t completed"
Thanks in advance for any help!
Solved! Go to Solution.
That would because I was silly and didn't test it properly. Try this one
import arcpy
import os
from arcpy import env
# set variables
path = r'\\gisfile\GISmaps\AtlasMaps\ATLAS_MAPS_17\New folder'
# Write to text file
txtFile = open(path + "{}".format("BrkSrceLst.txt"), "w")
txtFile.write("MXDs that have broken source data" + "\n")
txtFile.write("----------------------------------------------" + "\n")
# iterates through folder and lists broken layers
for fileName in os.listdir(path):
fullPath = os.path.join(path,fileName)
if os.path.isfile(fullPath) and fileName[-3:].lower() == 'mxd':
mxd = arcpy.mapping.MapDocument(fullPath)
txtFile.write("Checking " + fileName + " (" + fullPath + ") \n")
print "Checking " + fileName + " (" + fullPath + ")"
for brknList in arcpy.mapping.ListBrokenDataSources(mxd):
txtFile.write("\t broken layer: " + brknList.name + "\n")
print "\t broken layer: " + brknList.name
# iterates through folder and checks for broken picture element source
for elem in arcpy.mapping.ListLayoutElements(mxd, "PICTURE_ELEMENT", "*logo*"):
txtFile.write("\t broken picture element name: " + elem.name + "\n")
print "\t broken picture element name: " + elem.name
txtFile.write("----" + fileName + " Completed----\n")
txtFile.write("----All MXDs Completed----")
txtFile.close()
print "----All MXDs Completed----"
.
This is what my output looks like:
MXDs that have broken source data
----------------------------------------------
Checking ForPremiseAddress.mxd (C:\WorkDoc\Projects\NG911 Data\ForPremiseAddress.mxd)
broken layer: Vector_PremiseAddress
broken layer: Addresses_PreviousLoad
broken layer: Addresses
----ForPremiseAddress.mxd Completed----
Checking QAQC_VISUAL.mxd (C:\WorkDoc\Projects\NG911 Data\QAQC_VISUAL.mxd)
broken layer: VECTOR_PremiseAddress
broken layer: 8-26-2016
broken layer: NG911 CENTERLINES
broken layer: NG911 CENTERLINES_direction
broken layer: ng911_previous_load
broken layer: VECTOR_CenPWC
broken layer: PWCStreets_RR
broken layer: QMCBBND
broken layer: VECTOR.ZipPWC
broken layer: RegionalPostalCities
----QAQC_VISUAL.mxd Completed----
Checking Untitled.mxd (C:\WorkDoc\Projects\NG911 Data\Untitled.mxd)
broken layer: NVRRCL Boundary XY
broken layer: Addresses
broken layer: VECTOR_PremiseAddress
broken layer: Vector_CenPWC
broken layer: Roads
broken layer: PSAP_Boundary
broken layer: msag_prwm_va$
----Untitled.mxd Completed----
----All MXDs Completed----
I believe your problem is in line 27 and line 29. Try
import arcpy import os from arcpy import env # set variables path = r'\\gisfile\GISmaps\AtlasMaps\ATLAS_MAPS_17\New folder' # Write to text file txtFile = open(path + "{}".format("BrkSrceLst.txt"), "w") txtFile.write("MXDs that have broken source data" + "\n") txtFile.write("----------------------------------------------" + "\n") # iterates through folder and lists broken layers for fileName in os.listdir(path): fullPath = os.path.join(path,fileName) if os.path.isfile(fullPath): basename,extension = os.path.splitext(fullPath) if extension == ".mxd": mxd = arcpy.mapping.MapDocument(fullPath) print "current MXD being checked is: " + fileName for brknList in arcpy.mapping.ListBrokenDataSources(mxd): print "\t broken layer: " + brknList.name # iterates through folder and checks for broken picture element source for elem in arcpy.mapping.ListLayoutElements(mxd, "PICTURE_ELEMENT", "*logo*"): print "\t broken picture element name: " + elem.name txtFile.write(fullPath)
txtFile
.write(elem.name)
txtFile.close() print "\t completed"
@ Kevin
Thanks! Much more orderly than what I had. It successfully printed to a text file, but it's sort of hard to read because it's al bunched together. I know there's a way to get it to list in a vertical fashion as well as print the strings "current mxd being checked", "broken layer", "broken picture element name".
Think I've pointed you to this addin in another thread (the graphic broken link), butmy addin writes to .csv, .txt, and excel. If you unzip and look at the scripts you might be able to see how I did it. Fwiw
/blogs/myAlaskaGIS/2015/08/31/python-addin-for-data-inventory-and-broken-link-repair
Try this. This does two things. Formats the output file nicer. Secondly, the broken layer links where never written to the file only the pictures. And those pictures were only written if there was a broken layer. Hopefully it works for you.
*note: edited this post to fix a couple typos in code.
import arcpy
import os
from arcpy import env
# set variables
path = r'\\gisfile\GISmaps\AtlasMaps\ATLAS_MAPS_17\New folder'
# Write to text file
txtFile = open(path + "{}".format("BrkSrceLst.txt"), "w")
txtFile.write("MXDs that have broken source data" + "\n")
txtFile.write("----------------------------------------------" + "\n")
# iterates through folder and lists broken layers
for fileName in os.listdir(path):
fullPath = os.path.join(path,fileName)
if os.path.isfile(fullPath) and fileName[-3:] == 'mxd':
mxd = arcpy.mapping.MapDocument(fullPath)
txtFile.write("Checking " + filename + " (" + fullPath + ") \n")
print "Checking " + filename + " (" + fullPath + ")"
for brknList in arcpy.mapping.ListBrokenDataSources(mxd):
txtFile.write("\t broken layer: " + brknList.name + "\n")
print "\t broken layer: " + brknList.name
# iterates through folder and checks for broken picture element source
for elem in arcpy.mapping.ListLayoutElements(mxd, "PICTURE_ELEMENT", "*logo*"):
txtFile.write("\t broken picture element name: " + elem.name + "\n")
print "\t broken picture element name: " + elem.name
txtFile.write("----" + filename + " Completed----\n"")
txtFile.write("----All MXDs Completed----")
txtFile.close()
print "----All MXDs Completed----"
@ Kevin
Thanks again. I put what you came up with word for word, but somehow none of the print statements were printed to the window except line 29 (of your syntax)? I think you nailed the premise, however. It looks set up to work how I want it to.
And this is how it wrote to the txt document:
MXDs that have broken source data
----------------------------------------------
---WILL_HOUSE_DISTRICTS_A.mxdcompleted------WILL_HOUSE_DISTRICTS_B.mxdcompleted------All MXDs Completed---
That would because I was silly and didn't test it properly. Try this one
import arcpy
import os
from arcpy import env
# set variables
path = r'\\gisfile\GISmaps\AtlasMaps\ATLAS_MAPS_17\New folder'
# Write to text file
txtFile = open(path + "{}".format("BrkSrceLst.txt"), "w")
txtFile.write("MXDs that have broken source data" + "\n")
txtFile.write("----------------------------------------------" + "\n")
# iterates through folder and lists broken layers
for fileName in os.listdir(path):
fullPath = os.path.join(path,fileName)
if os.path.isfile(fullPath) and fileName[-3:].lower() == 'mxd':
mxd = arcpy.mapping.MapDocument(fullPath)
txtFile.write("Checking " + fileName + " (" + fullPath + ") \n")
print "Checking " + fileName + " (" + fullPath + ")"
for brknList in arcpy.mapping.ListBrokenDataSources(mxd):
txtFile.write("\t broken layer: " + brknList.name + "\n")
print "\t broken layer: " + brknList.name
# iterates through folder and checks for broken picture element source
for elem in arcpy.mapping.ListLayoutElements(mxd, "PICTURE_ELEMENT", "*logo*"):
txtFile.write("\t broken picture element name: " + elem.name + "\n")
print "\t broken picture element name: " + elem.name
txtFile.write("----" + fileName + " Completed----\n")
txtFile.write("----All MXDs Completed----")
txtFile.close()
print "----All MXDs Completed----"
.
This is what my output looks like:
MXDs that have broken source data
----------------------------------------------
Checking ForPremiseAddress.mxd (C:\WorkDoc\Projects\NG911 Data\ForPremiseAddress.mxd)
broken layer: Vector_PremiseAddress
broken layer: Addresses_PreviousLoad
broken layer: Addresses
----ForPremiseAddress.mxd Completed----
Checking QAQC_VISUAL.mxd (C:\WorkDoc\Projects\NG911 Data\QAQC_VISUAL.mxd)
broken layer: VECTOR_PremiseAddress
broken layer: 8-26-2016
broken layer: NG911 CENTERLINES
broken layer: NG911 CENTERLINES_direction
broken layer: ng911_previous_load
broken layer: VECTOR_CenPWC
broken layer: PWCStreets_RR
broken layer: QMCBBND
broken layer: VECTOR.ZipPWC
broken layer: RegionalPostalCities
----QAQC_VISUAL.mxd Completed----
Checking Untitled.mxd (C:\WorkDoc\Projects\NG911 Data\Untitled.mxd)
broken layer: NVRRCL Boundary XY
broken layer: Addresses
broken layer: VECTOR_PremiseAddress
broken layer: Vector_CenPWC
broken layer: Roads
broken layer: PSAP_Boundary
broken layer: msag_prwm_va$
----Untitled.mxd Completed----
----All MXDs Completed----
Kevin,
Thanks a bunch man. Works like a charm. I plan to make this into a script tool.