looping over the files in a directory

Daniel 'Dang' Griffith noemail at noemail4u.com
Mon May 17 09:01:37 EDT 2004


On Fri, 14 May 2004 15:32:32 GMT, "Elaine Jackson"
<elainejackson7355 at home.com> wrote:

>Can anyone please tell me the actual syntax for saying something like this:
>
>for <file> in <directory>:
>    <do something with the file>
>
>(?) Muchas gracias.
>
>Peace

Mark Nenadov posted a solution for a single directory.  If you want to
do so recursively, try something like this:

import os
for root, d, files in os.walk("."):
    for basename in files:
        filename = "%s/%s" % (root, basename)
        print filename # or open it, or whatever...

    --dang



More information about the Python-list mailing list