[Tutor] randomness

Andrew Wilkins toodles@yifan.net
Sat, 16 Jun 2001 15:22:57 +0800


Hi folks

What's a good way to generate a random unique file name? I can't use
os.tempnam/os.tmpnam, are they *nix specific? I'm using Windows ME with
Python 2.1 I could only think of this:

import os,random

files=os.listdir('threadhtml')
filename=''
while not filename:
 n=random.randint(0,100000)
 if not '%i.html' in files:
  filename='%i.html'

But it seems a bit yucky...can someone shed any light on the matter?

Thanks,

Andrew