I need to extract a list of all the files and folders within a top level folder. My first thought was to use os.walk in a small python script as it is a simple task. However, I cannot run scripts on that folder as it is on the main corporate server.
I then took the same script, placed it in a Calculate Value inside a model and ran it successfully (the server allows all connections from ArcGIS). This still didn't give the result I wanted as I needed to catalog the spatial datasets as well.
I ended up using arcpy.da.Walk and wrote the list of files out to a text file. I'm having a problem with the new line character though, as was discussed in a fair amount of detail in this question. Every time I try to add it in, it gets stripped out immediately and an error pops up. Right now the code looks like this:
for filename in filenames:
f.write(os.path.join(dirpath, filename))
It's writing each filename to the text file in "run-on" fashion, so it's quite useless. Is there some other way that I can insert a new line?
edit The issue at hand is forcing the inclusion of a newline character inside the model. I just happened to come across it while trying to create an "inventory". My question is not about how to get the list of files, but how to get the spacing to work correctly.
\nor\r\nafter your item to make a new line.One thing I suggest is using print's alternate syntax, like
– Jason Scheirer Feb 11 '13 at 06:26print >>f, os.path.join(dirpath, filename)Calculate Valuetool as well, and not justCalculate Field. – Cindy Jayakumar Feb 11 '13 at 07:24chr(10), as in"Mystring" + chr(10). If it requires windows-style line endings, trychr(13) + chr(10). – Jason Scheirer Feb 11 '13 at 07:54