かんたん作成【HTML CSS JavaScript PHP CGI Perl Ruby Python .htaccess】

ディレクトリ内ファイル一覧表示(listdir)

#! /usr/bin/python
# coding: Shift_JIS

import os
import re

#================================================
DIR = "../../sample/"                                    #一覧表示するディレクトリ

#================================================
def print_file(dir):
    for f in os.listdir(dir):                            #ファイル名取得
        fpath = dir + f
        if os.path.isdir(fpath):                        #ディレクトリの場合
            print_file(fpath + "/")
        elif re.search("\.s*html$", f) and os.path.getsize(fpath) > 100:    #拡張子、ファイルサイズ確認
            fname = re.sub(DIR, "", fpath)                    #ファイル名
            print """
                <a href="%s">%s</a> (%dByte)<br>
            """ % (fpath, fname, os.path.getsize(fpath))

#================================================
print "Content-type: text/html"
print """
    <html>
    <head>
    <title>ディレクトリ内ファイル表示</title>
    </head>
    <body>
"""

print_file(DIR)

print """
    </body>
    </html>
"""
かんたん作成【HTML CSS JavaScript PHP CGI Perl Ruby Python .htaccess】