# (C) Beuc 2019 import sys import os, urllib.parse, html, io # This function based on /usr/lib/python3.5/http/server.py def list_directory(path): list = os.listdir(path) list.sort(key=lambda a: a.lower()) r = [] try: displaypath = urllib.parse.unquote(path, errors='surrogatepass') except UnicodeDecodeError: displaypath = urllib.parse.unquote(path) displaypath = html.escape(displaypath, quote=False) enc = sys.getfilesystemencoding() title = 'Directory listing for %s' % displaypath r.append('') r.append('\n') r.append('' % enc) r.append('%s\n' % title) r.append('\n

%s

' % title) header_path = os.path.join(path, 'HEADER.html'); if os.path.exists(header_path): r.append(open(header_path, 'r').read()) r.append('
\n\n
\n\n\n') encoded = '\n'.join(r).encode(enc, 'surrogateescape') f = io.open(os.path.join(path, 'index.html'), 'wb') f.write(encoded) f.close() for dir in sys.argv[1::]: if os.path.isdir(dir): print("Generating", os.path.join(dir, 'index.html')) list_directory(dir) else: print("Not a directory:", dir)