os.walk() dirs and files
Tim Peters
tim.peters at gmail.com
Wed Feb 8 14:01:00 EST 2006
[rtilley]
> When working with file and dir info recursively on Windows XP. I'm going
> about it like this:
>
> for root, dirs, files in os.walk(path):
> for f in files:
> ADD F to dictionary
> for d in dirs:
> ADD D to dictionary
>
> Is it possible to do something such as this:
>
> for root, dirs, files in os.walk(path):
> for f,d in files, dirs:
> ADD F|D to dictionary
>
> Just trying to save some lines of code and thought it wise to ask the
> gurus before trying it :)
As has been pointed out,
for name in dirs + files:
is simple and effective. In a context where you don't want to endure
the memory burden of materializing a concatentated list, you can do
for name in itertools.chain(dirs, files):
intead.
More information about the Python-list
mailing list