[Tutor] random file rotation program

alan.gauld@bt.com alan.gauld@bt.com
Wed Mar 26 12:58:42 2003


> def gangstrip(thing):                   # ok, everybody STRIP!
>     index=0                                # This strips out 
>     while index < len(thing):      # define exit
>         thing[index]=string.strip(thing[index])
>         index=index+1  # increase the counter
>     return thing
> #
> if os.path.exists(targetfile):
>     f1.open(targetfile,'r')
>     cookies=gangstrip(f1.readlines())

I think this can be replaces by a list comprehension

cookies = [line.strip() for line in f1.readlines()]

But I seem to recall you are stuck on Python 1.5.1 in which 
case your gangstrip() function is OK.

> print random.choice(cookies)

You don;t say if theres a problem. It looks opk to me.
Reads in the cookie file, strips off surpluss whitespace then 
randomly selects one for printing...

Its probably faster to randomly select an index and only strip that 
line - another approach....

cookies = f1.readlines()
cookie = random.choice(cookies)
print string.strip(cookie)

But if you want the cookie cache to be used later preprocessing 
might be better.

Alan g.