Question: I would like to find all the files in a directory and all sub-directories.

code used:

```
import os
import sys

path = "C:\\"
dirs = os.listdir(path)
filename = "C.txt"
FILE = open(filename, "w")
FILE.write(str(dirs))
FILE.close()
print dirs

```

The problem is - this code only lists files in directories, not sub-directories. What do I need to change in order to also list files in subdirectories?

Answer: 
To traverse a directory tree you want to use [os.walk()](http://docs.python.org/library/os.html?highlight=os.walk#os.walk) for this.

Here's an example to get you started:

```
import os

searchdir = r'C:\root_dir' # traversal starts in this directory (the root) 

for root, dirs, files in os.walk(searchdir):
    for name in files:
        (base, ext) = os.path.splitext(name) # split base and extension
        print base, ext

```

which would give you access to the file names and the components.

You'll find the functions in the [os](http://docs.python.org/library/os) and [os.path](http://docs.python.org/library/os.path.html) module to be of great use for this sort of work.
This function will help you: os.path.walk() <http://docs.python.org/library/os.path.html#os.path.walk>