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.

  • 1
    What is the error? So adding "\n" doesn't work? – Nathan W Feb 11 '13 at 5:32
  • 1
    This is a standard Python language question, it is not GIS-specific. You just need to tack \n or \r\n after your item to make a new line. One thing I suggest is using print's alternate syntax, like print >>f, os.path.join(dirpath, filename) – Jason Scheirer Feb 11 '13 at 6:26
  • @JasonScheirer I know it is a Python issue, I guess I wasn't specific enough. I have tried escaping '\n' using the several ways available, as stated in the question I linked to. The difference is this seems to be a limitation in the Calculate Value tool as well, and not just Calculate Field. – Cindy Jayakumar Feb 11 '13 at 7:24
  • 1
    Try chr(10), as in "Mystring" + chr(10). If it requires windows-style line endings, try chr(13) + chr(10). – Jason Scheirer Feb 11 '13 at 7:54
  • If you escape the '\n' it ceases to be a newline character but merely a 'slash-en'. – MappaGnosis Feb 11 '13 at 9:08