Walk thru each subdirectory from a top directory

Diez B. Roggisch deets at nospam.web.de
Mon Feb 26 16:46:43 EST 2007


silverburgh.meryl at gmail.com schrieb:
> i am trying to use python to walk thru each subdirectory from a top
> directory.  Here is my script:
> 
> savedPagesDirectory = "/home/meryl/saved_pages/data"
> 
> dir=open(savedPagesDirectory, 'r')
> 
> for file in dir:
>     if (isdir(file)):
>         # get the full path of the file
>         fileName = savedPagesDirectory + file + 'index.html'
>         print fileName
> 
> $ ./scripts/regressionTest.py
> Traceback (most recent call last):
>   File "./scripts/regressionTest.py", line 12, in ?
>     dir=open(savedPagesDirectory, 'r')
> IOError: [Errno 21] Is a directory
> 
> But I get the above error:
> 
> Can you please tell me what did I do wrong?

You can't open a directory.

Use the function os.walk to do what you want.

Diez



More information about the Python-list mailing list