1

I start the server using:

python -m SimpleHTTPServer

In the browser, I can see the current directory listing by going to 'localhost'. However, it lists them in alphabetical order.

Can I have it in the Windows shell style, with subdirectories listed before files, but both groups individually sorted alphabetically?

1
  • Let me know whether its working or not Commented Oct 26, 2017 at 15:46

1 Answer 1

1

we need to make changes in SimpleHTTPServer.py file. I made some changes in the below function . You can replace and check

def list_directory(self, path):
    """Helper to produce a directory listing (absent index.html).

    Return value is either a file object, or None (indicating an
    error).  In either case, the headers are sent, making the
    interface the same as for send_head().

    """
    lst1=[]
    lst2=[]
    try:
        list = os.listdir(path)
        for i in list:
            if os.path.isdir(os.path.join(path,i)):
                lst1.append(i)
            elif os.path.isfile(os.path.join(path,i)):
                lst2.append(i)
    except os.error:
        self.send_error(404, "No permission to list directory")
        return None
    lst1.sort(key=lambda a: a.lower())
    #print lst1
    #print list
    lst2.sort(key=lambda a: a.lower())
    f = StringIO()
    displaypath = cgi.escape(urllib.unquote(self.path))
    f.write('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">')
    f.write("<html>\n<title>Directory listing for %s</title>\n" % displaypath)
    f.write("<body>\n<h2>Directory listing for %s</h2>\n" % displaypath)
    f.write("<hr>\n<ul>\n")
    for name in lst1:
        fullname = os.path.join(path, name)
        displayname = linkname = name
        # Append / for directories or @ for symbolic links
        if os.path.isdir(fullname):
            displayname = name + "/"
            linkname = name + "/"
        f.write('<li><a href="%s">%s</a>\n'
                % (urllib.quote(linkname), cgi.escape(displayname)))
    for name in lst2:
        fullname = os.path.join(path, name)
        displayname = linkname = name
        # Append / for directories or @ for symbolic links
        if os.path.islink(fullname):
            displayname = name + "@"
            # Note: a link to a directory displays with @ and links with /
        f.write('<li><a href="%s">%s</a>\n'
                % (urllib.quote(linkname), cgi.escape(displayname)))

    f.write("</ul>\n<hr>\n</body>\n</html>\n")
    length = f.tell()
    f.seek(0)
    self.send_response(200)
    self.send_header("Content-type", "text/html")
    self.send_header("Content-Length", str(length))
    self.end_headers()
    return f
Sign up to request clarification or add additional context in comments.

3 Comments

perfect! Thanks Sandeep. I modified your answer slightly to concatenate the 2 lists into 'list' and continue with the original code below that.
Could you also show how to add a link (like "..") at the top of the list, to point to the parent directory, when we have gone into a sub-directory?
Good to know :-) i will check about displaying .. and will let you know

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.