[Tutor] Accessing next and previous items during iteration

Chris or Leslie Smith smiles at worksmail.net
Fri Dec 16 15:55:25 CET 2005


| Is it possible to access the next and previous items during an
| iteration? 
| 
| I want to use it to iterate through html files in a folder and add
| links in to the next and previous pages.
|
I just did this :-) What I did was 

1) use the glob module to get a list of the files in the directory (*.htm)
2) sort this using a special sort function (in my case the files were named as #_# where # was a number and I wanted to sort according to the first number and subsort according to the second)
3) then I just stepped through the list using enumerate and used the index to find the next and previous names in the list, e.g.

####
for i, fil in enumerate(flist):
    if i<>1:
        prev = flist[i-1]
        #do previous link
    if i<> len(flist):
        next = flist[i+1]
        #do next link
####

/c



More information about the Tutor mailing list