os.walk() dirs and files

R

rtilley

Hello,

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 :)

Thanks!
 
R

rtilley

rtilley said:
Hello,

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 to clarify. In this particular case, I do not need to differentiate
between files and dirs... so would it be possible to do something such
as this:

for root, dirs, files in os.walk(path):
for fs_object in files, dirs:
ADD fs_object to dictionary
 
D

Duncan Booth

rtilley said:
Just to clarify. In this particular case, I do not need to differentiate
between files and dirs... so would it be possible to do something such
as this:

How about just concatentating the two lists:
for root, dirs, files in os.walk(path):
for fs_object in files, dirs:
for fs_object in files + dirs:
 
G

George Sakkis

rtilley said:
Thank you Duncan! that solves the problem perfectly!

Or a bit more efficiently (no need to allocate a new list for storing
files+dirs):

from itertools import chain
for root, dirs, files in os.walk(path):
for fs_object in chain(files,dirs):
ADD fs_object to dictionary

Or you can download the path.py module
(http://www.jorendorff.com/articles/python/path/):

from path import path
for fs_object in path(root).walk():
ADD fs_object to dictionary


George
 
T

Tim Peters

[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.
 
R

rtilley

George said:
Or a bit more efficiently (no need to allocate a new list for storing
files+dirs):

from itertools import chain
for root, dirs, files in os.walk(path):
for fs_object in chain(files,dirs):
ADD fs_object to dictionary

I like that! itertools is cool... a bit abstract and computer sciencey,
but it sure does work in a practical manner :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top