5

I am trying to list directories and files (recursivley) in a directory with python:

./rootdir
  ./file1.html
  ./subdir1
    ./file2.html
    ./file3.html
  ./subdir2
  ./file4.html

Now I can list the directories and files just fine (borrowed it from here). But I would like to list it in the following format and ORDER (which is very important for what I am doing.

/rootdir/
/rootdir/file1.html
/rootdir/subdir1/
/rootdir/subdir1/file2.html
/rootdir/subdir1/file3.html
/rootdir/subdir2/
/rootdir/file4.html

I don't care how it gets done. If I walk the directory and then organize it or get everything in order. Either way, thanks in advance!

EDIT: Added code below.

# list books
import os
import sys

lstFiles = []
rootdir = "/srv/http/example/www/static/dev/library/books"

# Append the directories and files to a list
for path, dirs, files in os.walk(rootdir):
    #lstFiles.append(path + "/")
    lstFiles.append(path)
    for file in files:
        lstFiles.append(os.path.join(path, file))

# Open the file for writing
f = open("sidebar.html", "w")
f.write("<ul>")

for item in lstFiles:
    splitfile = os.path.split(item)
    webpyPath = splitfile[0].replace("/srv/http/example/www", "")
    itemName = splitfile[1]
    if item.endswith("/"):
        f.write('<li><a href=\"' + webpyPath + "/" + itemName + '\" id=\"directory\" alt=\"' + itemName + '\" target=\"viewer\">' + itemName + '</a></li>\n')
    else:
        f.write('<li><a href=\"' + webpyPath + "/" + itemName + '\" id=\"file\" alt=\"' + itemName + '\" target=\"viewer\">' + itemName + '</a></li>\n')

f.write("</ul>")
f.close()
Community
  • 1
  • 1
Milo Gertjejansen
  • 511
  • 1
  • 7
  • 22
  • 3
    "If I walk the directory" using `os.walk` that's exactly what you get. Have you tried calling `os.walk()`? If not, please write some code and ask **specific** questions about that code. If you have tried using `os.walk()` what questions do you have about the code you've written? – S.Lott Sep 19 '11 at 23:31
  • 1
    What is this "ORDER" you are so keen on? Do you mean that the paths should be ordered by the filename at the end? What do you do if subdir1 contains file1.html and file3.html, but subdir2 contains file2.html? – Tom Anderson Sep 19 '11 at 23:36
  • @S.Lott I just tried it out and I believe it is my code that is wrong, but I have no idea about how I would organize the for loops within the for loop `for path, dirs, files in os.walk("."):`. My code can be found [here](http://pastebin.com/V7m0e2z2). – Milo Gertjejansen Sep 19 '11 at 23:46
  • @TomAnderson The order I need is just if you were following the tree down. So if you got to a directory, you would go into the directory and then follow that down until the end of the directory. After that, you would go up a directory level and just continue on. The example I gave is merely an example (my tree is huge). – Milo Gertjejansen Sep 19 '11 at 23:48
  • @Milo Gertjejansen: Please actually post your actual code as part of the actual question. Please do not add comments to your question. Please **update** your question to be complete. – S.Lott Sep 20 '11 at 00:23
  • Milo Gertjejansen: What is wrong with your code? "I just tried it out and I believe it is my code that is wrong"? What's wrong with that code? Please **update** the question to be very specific on the problems or errors you are having. Reading a bunch of comments is hard. Reading one complete question is easier. – S.Lott Sep 20 '11 at 02:31

1 Answers1

5

Try the following:

for path, dirs, files in os.walk("."):
    print path
    for file in files:
        print os.path.join(path, file)

You do not need to print entries from dirs because each directory will be visited as you walk the path, so you will print it later with print path.

Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
  • One more thing though, semi-unrelated: Is there an easy way to differentiate between files and folders (using your model, instead of printing, I append it to a list) within that list? – Milo Gertjejansen Sep 20 '11 at 00:08
  • 1
    You could append a "/" to `path` when you add it to the list, that way all folders would end with a "/" but none of the files would. – Andrew Clark Sep 20 '11 at 16:16
  • 1
    Alternatively, instead of storing strings, you could store a tuple (string, bool), where the boolean is a flag indicating if that path is a directory or not. – Tom Anderson Sep 20 '11 at 17:16