How do I loop a folder of mxds

14753
43
05-10-2016 08:23 AM
DevinUnderwood2
Occasional Contributor

I want to loop many mxds within a folder to retrieve layer,connection,etc. information using python.

I just need the syntax for looping through the mxds.

0 Kudos
43 Replies
DanPatterson_Retired
MVP Emeritus

I hope so ... arcpy.AddMessage('!!! Success !!!  ')

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

Thanks Dan.  culdn't remember if it was AddMessage or Addmessage, etc 

The two custom mods should be commented out too, but that may break things if trying to run this as a standalone.  It's meant to be in the toolbox, but still should have the required csv pieces.

Also, written last year when my print formats and other tricks I've learned since are not used.  One of those..."when I have time, I'll make it nicer" things.  but works for me, and works in the toolbox, so....

DevinUnderwood2
Occasional Contributor

I went ahead and arrived at this, using built in python. This performs perfect as a test. However, I have an issue when setting a returned list as a reader then writing.

The error is AttributeError: 'unicode' object has no attribute 'readlines.   I think it has an issue with passing the list in its current encoded state ?

#Open Excel Docuements
readexcel = open ('xxx.csv','r')
writeexcel = open ('yyy.csv','w')

#Set Reader & Writer
reader = readexcel.readlines()
writer = writeexcel.writelines (reader)

#Loop Read Excel Document  to print to Write Excel Docuement
for line in (readexcel):
    line = readexcel.next()
    print (line)

#Close Excel Files
readexcel.close()
writeexcel.close()

0 Kudos
GerardHavik
New Contributor III

Devin,

As soon as I found something that worked I stopped searching.

For reading and writing I use mostley simple 'object=open(file,mode)' and for reading I loop over the file one line at the time 'for line in object:'. Very compact and simple code. Writing is 'object.write(reg) where 'reg' is a string.

Readline() is more complicated I think. See

https://docs.python.org/2/tutorial/inputoutput.html#methods-of-file-objects

The csv module is useful for reading parts of complicated csv-files, it produces a list of dictionaries. It is possible that it makes more elegant code for writing csv-files than what I used so far, if there is time I'l try, never to old to learn.

PS, my code uses the semicolon as delimiter, I'm from Holland. You want to change it to colon I think.

Met vriendelijke groet,

G.J. (Gerard) Havik

Dataspecialist

een gedachte voor het milieu: is printen van deze mail echt nodig?

Verstuurd vanaf mijn iPad

Op 23 mei 2016 om 22:37 heeft Devin Underwood <geonet@esri.com<mailto:geonet@esri.com>> het volgende geschreven:

GeoNet <https://community.esri.com/?et=watches.email.thread>

How do I loop a folder of mxds

reactie van Devin Underwood<https://community.esri.com/people/EscondidoAnalyst?et=watches.email.thread> in Python - Bekijk de volledige discussie<https://community.esri.com/message/610396?et=watches.email.thread#comment-610396>

0 Kudos