[SciPy-User] Curse of recursive directory walking

josef.pktd at gmail.com josef.pktd at gmail.com
Wed Jul 29 10:48:47 EDT 2009


On Wed, Jul 29, 2009 at 10:30 AM, Gökhan SEVER<gokhansever at gmail.com> wrote:
> I have a simple Python script which is written with a purpose to walk
> through the given root directory and spot and read the right data files and
> plot some of the desired variables within them for the first stage data
> inspection.
>
> The script named (dccn_plot_all.py) is given as below --while dccn_plot
> being the main action script:
>
>
> #!/usr/bin/env python
>
> # dccn_plot_all
>
> import os
> from subprocess import call
>
> init = os.getcwd()
>
> for root, dirs, files in os.walk('.'):
>     for file in files:
>         if file.split('.')[1] == 'sau' and not file.endswith('.nc'):
>             print file
>             os.chdir(root)
>             print os.getcwd()
>             call(['dccn_plot', file])
>             os.chdir(init)
>
>
> Here is the fun part: While I was testing the script, I mistakenly called
> this dccn_plot_all script itself within the os.walk suite. And this was
> resulted with a almost complete lose of my Bash shell access on my Fedora 11
> and made windows manager also unreachable. To recover, I had to restart the
> system.
>
> The lesson learnt: be extremely cautious while working in the nearby of
> recursive functions.

To me it looks more like it is a problem with relative file paths and
changing directories and not with recursion.
I never had problems with os.walk or any home made recursive directory
walker, but I always used absolute paths.

from the python 2.5 docs

Note: If you pass a relative pathname, don't change the current
working directory between resumptions of walk(). walk() never changes
the current directory, and assumes that its caller doesn't either.

Josef

>
> --
> Gökhan
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>



More information about the SciPy-User mailing list