random function with strings
Adam Ruth
owski at hotmail.com
Sun Jun 22 01:46:40 EDT 2003
ataraxia2500 <messageboardfan1 at yahoo.com> wrote in message news:<3ef51a51$0$8029$79c14f64 at nan-newsreader-01.noos.net>...
> I have a function that goes through a directory and I would like to remove
> randomly one of the files contained in that directory. I checked out the
> random() function in the documentation but it only deals with integers.
>
> here's what my function looks like:
>
> with os.path.walk
> def delete_backups(arg, dirname, names):
> /*here I need a function that select a file randomly and, let's call it
> "name", then I do this:*/
> os.remove(os.path.join(dirname, name))
>
> any suggestions?
>
> thanx in advance
The random module has exactly what you need:
def delete_backups(arg, dirname, names):
name = random.choice(names)
os.remove(os.path.join(dirname, name))
Adam Ruth
More information about the Python-list
mailing list