random function with strings
Tim Peters
tim.one at comcast.net
Sat Jun 21 23:00:51 EDT 2003
[messageboardfan1 at yahoo.com]
> 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.
random.choice(seq) returns an element from the its sequence argument.
> 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?
Do
random.choice(names)
to pick a name at random. Note that this will fail if names is empty, and
won't do what you hope for if the name returned happens to be the name of a
contained directory.
More information about the Python-list
mailing list