I need to display directory contents on GH Pages.
Would prefer
Better is this way which will go into subdirectory and make index files for them as well
""" Build index from directory listing
make_index.py [--header ]
"""
from __future__ import print_function
import os.path, time
INDEX_TEMPLATE = r"""
${header}
Index of ${header}
Name
Last modified
Size
Description
Parent Directory
-
% for name in dirnames:
${name}
${time}
-
% endfor
% for name in filenames:
${name}
${time}
-
% endfor
"""
EXCLUDED = ['index.html']
import os
import argparse
# May need to do "pip install mako"
from mako.template import Template
def fun(dir,rootdir):
print('Processing: '+dir)
filenames = [fname for fname in sorted(os.listdir(dir))
if fname not in EXCLUDED and os.path.isfile(dir+fname)]
dirnames = [fname for fname in sorted(os.listdir(dir))
if fname not in EXCLUDED ]
dirnames = [fname for fname in dirnames if fname not in filenames]
# header = os.path.basename(dir)
f = open(dir+'/index.html','w')
print(Template(INDEX_TEMPLATE).render(dirnames=dirnames,filenames=filenames, header=dir,ROOTDIR=rootdir,time=time.ctime(os.path.getctime(dir))),file=f)
f.close()
for subdir in dirnames:
try:
fun(dir+subdir+"/",rootdir+'../')
except:
pass
def main():
parser = argparse.ArgumentParser()
parser.add_argument("directory")
parser.add_argument("--header")
args = parser.parse_args()
fun(args.directory+'/','../')
if __name__ == '__main__':
main()