A little assistance with os.walk please.

Tim Chase python.list at tim.thechases.com
Mon Aug 14 11:11:53 EDT 2006


> The os.walk function walks the operating systems directory tree.

Yup.

> This seems to work, but I don't quite understand the tupple that is
> returned...
> Can someone explain please?
> 
> for root, dirs, files in os.walk('/directory/'):
> 	print root
> #	print dirs
> #	print files


As cheesy as it may sound, try uncommenting the two commented 
lines.  For a more explicit variant:

	print repr(dirs)
	print repr(files)

You'll notice that they're lists.

In the directory "root", you'll find the subdirectories given in 
the list "dirs" and you'll find the files given in the list "files".

-tkc






More information about the Python-list mailing list