Welcome, guest | Sign In | My Account | Store | Cart

CGI Directory Contents was a simple experiment meant to work similarly to the os.listdir function in Python. This is committed for archival to be run under Python 2.5 or later versions.

Python, 34 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import os, Zcgi

def main():
    dirs = []
    files = []
    others = []
    for name in os.listdir(os.getcwd()):
        path = os.path.join(os.getcwd(), name)
        if os.path.isdir(path):
            dirs.append(name)
        elif os.path.isfile(name):
            files.append(name)
        else:
            others.append(name)
    text = underline('Current Directory Contents:')
    if len(dirs):
        text += '\n\n' + underline('Directories:')
        for name in dirs:
            text += '\n' + name
    if len(files):
        text += '\n\n' + underline('Files:')
        for name in files:
            text += '\n' + name
    if len(others):
        text += '\n\n' + underline('Others:')
        for name in others:
            text += '\n' + name
    Zcgi.print_plain(text)

def underline(string):
    return string + '\n' + '=' * len(string)

if __name__ == '__main__':
    Zcgi.execute(main, 'cgi')

The Zcgi module can be found in recipe 475112.