I have a list of directory paths and would like to create a combined list of files in these directories incl. full path name. I am iterating through the list of directories using glob.glob() to get the files with full path name and then append it to an output list but for some reason I end up with a list of lists instead a single list containing the files
fileli = []
for item in sourceli:
file = glob.glob(item + '/****-****-**-**.txt')
fileli.append(file)
this returns a list of e.g. 3 lists if I have three directories, like this [[filedir1.txt],[filedir2.txt],[filedir3.txt]]
Another approach I tried:
fileli.append([glob.glob(item + '/****-****-**-**.txt') for item in sourceli])
but it returns a list in a list in a list like this [[[filedir1.txt, filedir2.txt, filedir3.txt]]]
Would appreciate if someone could explain (in rather simple terms, beginner here) why I end up with the results I get for each approach and how to get a single list of files like this [filedir1.txt, filedir2.txt, filedir3.txt].
Thanks a lot!
Update:
Figured out what's happening with my code. Wasn't aware that file = glob.glob(item + '/****-****-**-**.txt')
will return a list and therefore iterating through my list of directories will get me a list that's then appended to the list of files. Adding another for-loop now gives me the result I was looking for.
for item in sourceli:
print(item)
files = glob.glob(path + '/****-****-**-**.txt')
for file in files:
fileli.append(file)