pythonhtmlstatic-site

Creating Nested Indexes of Subfolders using Python


I am using python to create an index.html page that lists all the files in each folder and subfolder of my static-website. I also want to create the file indexes for the subfolders and their contents.

I am having trouble directing my python program to the correct folder for each make_index_html action for each folder. I keep having to explicitly write out file locations in order to avoid getting a FileNotFoundError.

from pathlib import Path

def make_index_html( path ):
    path = Path(path)
    
    ## Navigate to inside current folder
    path = path / 'FOLDER A'

    with open( path / 'index.html', 'w' ) as out:

        for file in sorted(list(path.iterdir())):
            out.write( f'<a href="{file.name}">{file.name}</a>\n'  )

if __name__ == '__main__': 
     make_index_html('.')

## Create the index page for the subfolder
with open("./FOLDER A/SUBFOLDER B/index.py") as file:
     exec(file.read())

Here's an example of what I want my site to look like:

HOME INDEX

FOLDER A INDEX

SUBFOLDER D INDEX

The way I got things to work right now, I have to have an index.py file for every folder and subfolder.

I realize there has to be a better/simpler way to do this.

I admit this is such a mess because I'm not as familiar with python as I should be. Please feel free to direct me to concepts/functions that I should read up on as well! Thanks in advance!


Solution

  • Here is one potential approach that uses os.walk() as recommended by shaf shafiq. Note that a templating package like jinja might help you make the index.html files more robust.

    import os
    
    def write_file_listing_contents(root_dir):
        output_file_name = "index.html"
        do_not_index = {output_file_name, "some_other_file.py"}
        el_a_template = """<a href="{filename}">{filename}</a>"""
    
        for dir_path, dir_names, file_names in os.walk(root_dir):
            output_path = os.path.join(dir_path, output_file_name)
    
            ## ---------------
            ## We will not index some files
            ## ---------------
            file_names = [f for f in file_names if f not in do_not_index]
            ## ---------------
    
            with open(output_path, "w") as f:
                for filename in file_names:
                    f.write(el_a_template.format(filename=filename) + "\n")
    
    WEB_ROOT = "D:/temp/StackOverflow/foo"
    write_file_listing_contents(WEB_ROOT)