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

Ministeyr ministeyr at free.fr
Tue Mar 21 04:36:15 EST 2006


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.



More information about the Python-list mailing list