1

I have the elements stored in an array, but they are stored inside a list. How do I get them without the list involved? This is what I mean

for folders in os.listdir(folder_path):
    for folder in os.listdir(folder_path+folders):
        if folder == 'images':
            image_file = os.listdir(folder_path+folders+'/'+'images'+'/')
            allFileNames.append(image_file)`

What I get is

for i in allFileNames:
    print (i)


['a31deaf0ac279d5f34fb2eca80cc2abce6ef30bd64e7aca40efe4b2ba8e9ad3d.png']
['308084bdd358e0bd3dc7f2b409d6f34cc119bce30216f44667fc2be43ff31722.png']
['5953af5080d981b554529971903d8bee9871457a4361b51f04ba04f43793dd8f.png']

What I would ideally like is

'a31deaf0ac279d5f34fb2eca80cc2abce6ef30bd64e7aca40efe4b2ba8e9ad3d.png'
'308084bdd358e0bd3dc7f2b409d6f34cc119bce30216f44667fc2be43ff31722.png'
'5953af5080d981b554529971903d8bee``9871457a4361b51f04ba04f43793dd8f.png'
'29dd28df98ee51b4ab1a87f5509538ecc3e4697fc57c40c6165658f61b0d8e3a.png'

Any suggestions on how I may do this?

kmario23
  • 50,454
  • 13
  • 141
  • 141
Ryan
  • 6,149
  • 12
  • 34
  • 58

2 Answers2

3

I think you can just replace

allFileNames.append(image_file)

by

allFileNames.extend(image_file)

Check this thread for more details.

Cleb
  • 22,913
  • 18
  • 103
  • 140
  • It is very well explained in the thread I linked. `append` will add a list to an existing list while `extend` adds the elements to the existing list. So difference is that you end up with only one list instead of a list of lists. – Cleb Jan 21 '18 at 13:17
  • 1
    Thanks a lot @cleb – Ryan Jan 21 '18 at 13:18
1

You can try with Generators:

  allFileNames = (os.listdir(folder_path+folders+'/'+'images'+'/') 
                    for folders in os.listdir(folder_path) 
                    for folder in os.listdir(folder_path+folders) if folder == "images")
  print(*list(allFileNames),sep="\n") #In python 2, need to import this from __future__ import print_function 
Pradam
  • 773
  • 7
  • 16