should os.walk return a list instead of a tuple?

M

Ministeyr

Hello,

os.walk doc: http://www.python.org/doc/2.4/lib/os-file-dir.html#l2h-1625

When walking top to bottom, it allows you to choose the directories you
want to visit dynamically by changing the second parameter of the tuple
(a list of directories). However, since it is a tuple, you cannot use
"filter" on it, since it would mean reassigning it:

for dir_tuple in os.walk('/home'):
dir_tuple[1]=filter(lambda x: not x.startswith('.'), dir_tuple[1])
#do not show hidden files
print dir_tuple #just print the directory and its contents in the
simplest possible way

If os.walk did return a list of three items instead of a tuple, that
would become possible. It would also not break old code like
for dirpath, dirnames, filenames in os.walk(somedir):
do something.....
Since assigning a list to a tuple is valid python code.

Thanks.
 
D

Duncan Booth

Ministeyr said:
When walking top to bottom, it allows you to choose the directories
you want to visit dynamically by changing the second parameter of the
tuple (a list of directories). However, since it is a tuple, you
cannot use "filter" on it, since it would mean reassigning it:

for dir_tuple in os.walk('/home'):
dir_tuple[1]=filter(lambda x: not x.startswith('.'),
dir_tuple[1])
#do not show hidden files
print dir_tuple #just print the directory and its
contents in the
simplest possible way

If os.walk did return a list of three items instead of a tuple, that
would become possible.

But you don't need to assign to it, you simply need to mutate it:

for dir, subdirs, files in os.walk('/home'):
subdirs[:] = [d for d in subdirs if not d.startswith('.')]
print dir, subdirs, files

(and if you are desparate to use filter+lambda that works as well.)
 
B

bruno at modulix

Ministeyr said:
Hello,

os.walk doc: http://www.python.org/doc/2.4/lib/os-file-dir.html#l2h-1625

When walking top to bottom, it allows you to choose the directories you
want to visit dynamically by changing the second parameter of the tuple
(a list of directories). However, since it is a tuple, you cannot use
"filter" on it, since it would mean reassigning it:

for dir_tuple in os.walk('/home'):
dir_tuple[1]=filter(lambda x: not x.startswith('.'),
dir_tuple[1]) #do not show hidden files
print dir_tuple #just print the directory and its contents in
the simplest possible way

Ok, you are missing 2 points here :
1/ multiple assignment. Python allows you to do things like:
a, b, c = (1, 2, 3)

So the canonical use of os.walk is:
for dirpath, subdirs, files in os.walk(path):
...

2/ what's mutable and what is not: a tuple is immutable, but a list is
not. The fact that the list is actually an element of a tuple doesn't
make it immutable:
t = ('A', [1, 2, 3])
t ('A', [1, 2, 3])
t[1] [1, 2, 3]
# this will work
t[1].append(4)
t ('A', [1, 2, 3, 4])
# this won't work
t[1] = []
Traceback (most recent call last):

HTH
 

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

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top