Joe T. Boka
Joe T. Boka

Reputation: 6585

Can't print a generator expression in Python 3

I have tried print(list(....)) which should work but in this case, it doesn't.

I am creating a dictionary with directory names as keys and a list with the file names in each directory as the as values. I am using os.walk.

import os
d = {}
for x,y,z in os.walk(path_folders):
    for f in y:
        if f not in d:
            d[f] = []
        d[f].append(i for i in z)    

print (d)

I get the dictionary as an output but I can't print or loop through the values which are generator expressions. Again, I tried list and loop but it's not working.

{'folder4': [<generator object <genexpr> at 0x0000000004B8B798>], 'folder5': [<generator object <genexpr> at 0x0000000004B8BCF0>], 'subfolder6': [<generator object <genexpr> at 0x0000000004B95E10>], 'subfolder4': [<generator object <genexpr> at 0x0000000004B8B990>], 'subfolder5': [<generator object <genexpr> at 0x0000000004B95360>], 'forder2': [<generator object <genexpr> at 0x0000000004B8BC18>], 'folder6': [<generator object <genexpr> at 0x0000000004B8B510>], 'folder3': [<generator object <genexpr> at 0x0000000004B853A8>], 'folder1': [<generator object <genexpr> at 0x0000000004921CF0>]}

Upvotes: 2

Views: 186

Answers (3)

Jared Mackey
Jared Mackey

Reputation: 4158

Use extend instead of append. The problem is you aren't iterating through the generator to get the results, instead you are just putting the promise of results in the dictionary.

This code worked for me.

import os

path_folders = "/path/to/stuff"

d = {}
for x,y,z in os.walk(path_folders):
    for f in y:
        if f not in d:
            d[f] = []
        d[f].extend(i for i in z)

print (d)

Edit:

Ok I must have missed something earlier... The dirnames is a list of all directories in that directory, and filenames is just all the files in that one directory, not the sub directories. I knew something was off about it. So I limited the scope to a folder that I can easily see what files are in there and did some testing. We were making it more complex than it had to be.

import os

path_folders = "/path/to/stuff/limited"

d = {}
for dirpath, dirnames, filenames in os.walk(path_folders):
    d[dirpath] = filenames

print(d)

dirpath is the full path. dirnames is all the directories in that path. filenames is all the files in that path.

If you want the current folder name that all those paths are in you are going to need to trim dirpath to get it. dirpath.split('/')[-1] should do that for you.

With the above trimming and code I get these results.

{'directory1': ['file1', 'file2'], 'directory2': ['file3', 'file4']}

Upvotes: 1

furas
furas

Reputation: 142641

Use d[f] = z in place of d[f].append(i for i in z)

import os

d = {}

for x, y, z in os.walk(path_folders):
    for f in y:
        d[f] = z    

print (d)

or

import os

d = {}

for x, y, z in os.walk(path_folders):
    for f in y:
        if f not in d:
            d[f] = []
        d[f] += z    

print (d)

Upvotes: 0

Mangu Singh Rajpurohit
Mangu Singh Rajpurohit

Reputation: 11420

Try this

import os
d = {}
for x,y,z in os.walk(path_folders):
    for f in y:
        if f not in d:
            d[f] = []
        d[f].extend([i for i in z])    

print (d)

Upvotes: 0

Related Questions