Search code examples
pythonminio

random.choice prints always the same result


i'm pretty new to python but i know how to use most of the things in it, included random.choice. I want to choose a random file name from 2 files list.

To do so, i'm using this line of code:

minio = Minio('myip',
                access_key='mykey',
                secret_key='mykey',
              )

images = minio.list_objects('mybucket', recursive=True)

for img2 in images:
    names = img2.object_name

print(random.choice([names]))

Everytime i try to run it, it prints always the same file's name (c81d9307-7666-447d-bcfb-2c13a40de5ca.png)

I tried to put the "print" function in the "for" block, but it prints out both of the files' names


Solution

  • You are setting the variable names to one specific instsance of images right now. That means it is only a single value. Try adding them to an array or similar instead.

    For example:

    names = [img2.object_name for img2 in images]
    
    print(random.choice(names))