[Tutor] ok, I want to read a dir.

Michael P. Reilly arcege@speakeasy.net
Fri, 28 Dec 2001 10:22:42 -0500


On Thu, Dec 27, 2001 at 08:45:50PM -0500, Kirk Bailey wrote:
> 
> ok, this is improving, still WRONG, but much closer.
> 
[beginning code snipped]
> def descriptor(filename):                       # Read 1st line in file
> named.
>         f1=open(filename,'r')                   #open the file named,
>         info=string.strip(f1.readline())        # read the first line in
> the file
>         f1.close()                              # close the file
>         return info                             # and exit returning the
> 1 line read
> #
> 
> filelist = glob.glob('./lists/*.info')
> #
> for x in filelist:                              # filelist lists all the
> available info files
>         #
>         print "<a href= \"" + webpath + "cgi-bin/commander.py?list=" + x
> +  "\">" + x + "</a> ", descriptor(x) +'<P>'
> #which means:
> #      print 'a
> href="http://www.howlermonkey.net/cgi-bin/commander.py?list=(listname)">
> (description)<p>'
> #
[rest snipped]

Okay, you need the pathname to get the descriptor, but you do not want
the path in the HTML.  Got it finally. :)

  filelist = glob.glob('./lists/*.info')
  for path in filelist:
    fname = os.path.basename(path)  # get just '*.info'
    url = webpath + 'cgi-bin/commander.py?list=' + fname  # I would
                      # put this on a separate line for clarity
    print '<a href="%s">%s</a>' % (url, fname), descriptor(path) + "<P>"

It might also be that you want to get rid of the '.info' portion in the
anchor tag itself.

  filelist = glob.glob('./lists/*.info')
  for path in filelist:
    fname = os.path.basename(path)  # get just '*.info'
    basename = os.path.splitext(fname)[0]   # take off '.info'
    # I would put this on a separate variable for clarity
    url = webpath + 'cgi-bin/commander.py?list=' + fname
    print '<a href="%s">%s</a>' % (url, basename), descriptor(path) + '<P>'

You might also want to put some code in if there are no files in the
filelist list.

  -Arcege