2

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

  • Well, I don't know exactly what `images` is, but it seems to me that you set `names` to be a single thing. Then your pick a random element from the *one-element list* `[names]`. – gspr Jul 01 '20 at 09:06
  • "images" is the list of the objects uploaded to my database. I want to pick a random file's name from that list – MATTIA ANTONACCI Jul 01 '20 at 09:08
  • Then you might wanna do `random.choice(images).object_name` instead. You're currently defining `names` to be first the first object in `images`, then the second, etc., so at the end of the loop, `names` is the (name of the) *last* object in `images`. You then select a random element in the *one-element* list `[names]`, which obviously must give that single element. – gspr Jul 01 '20 at 09:10
  • Now it gives me this error: `Traceback (most recent call last): File "C:/Users/Mattia Antonacci/Desktop/Robe/MinioRipetra/test1.py", line 17, in images = random.choice(immagini).object_name File "C:\Users\Mattia Antonacci\AppData\Local\Programs\Python\Python38-32\lib\random.py", line 288, in choice i = self._randbelow(len(seq)) TypeError: object of type 'generator' has no len()` – MATTIA ANTONACCI Jul 01 '20 at 09:12
  • So `images` is in fact *not* a list then. – gspr Jul 01 '20 at 09:13
  • i've solved the problem with @JakobVinkas 's answer, thank you too for helping me! – MATTIA ANTONACCI Jul 01 '20 at 09:15

4 Answers4

1

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))
JakobVinkas
  • 1,003
  • 7
  • 23
0
minio = Minio('myip',
                access_key='mykey',
                secret_key='mykey',
              )

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

for img2 in images:
    names.append(img2.object_name)

print(random.choice([names]))

Try this, the problem may that your names was not list varible

Danil Melnikov
  • 256
  • 1
  • 3
  • 11
0

If you want to choose one of the file names, try this :

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

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

names = []
for img2 in images:
    names.append(img2.object_name)

print(random.choice(names))

The first code tries to randomly select a value from the array, [names], but this only contain a single value, the value of the names variable after the last iteration of the for loop. Instead of doing that, create an array, names and append the values of img2.object_name from each iteration of the for loop to this array. And use this array to get the random name.

Roshin Raphel
  • 2,612
  • 4
  • 22
  • 40
0

Your names is just a simple variable that contains the most recently seen image name, rather than a list full of them.

The very last line of your script works as the one below:

value = 2
print(random.choice([value]))

which always prints 2. To get what you want, you need a list of all images. Here is your code with a simple fix:

names = []
for img2 in images:
    names.append(img2.object_name)

print(random.choice(names))

These code can be made considerably shorter with list comprehension:

names = [img2.object_name for img2 in images]
print(random.choice([names]))
tnorgd
  • 1,580
  • 2
  • 14
  • 24