How to walk up parent directories?

Diez B. Roggisch deets at nospam.web.de
Mon May 4 09:45:16 EDT 2009


Snorri H wrote:

> On May 4, 5:04 am, Matthew Wilson <m... at tplus1.com> wrote:
>> Is there already a tool in the standard library to let me walk up from a
>> subdirectory to the top of my file system?
> 
> 
> Never seen such a standard tool, yet it can be implemented in a way
> like this
> 
> def walkup(path):
>     aux = path.rsplit('/')
>     while aux != ['']:
>         yield reduce(lambda x,y:x+'/'+y,aux)
>         aux.pop()
>     yield '/'
> 
> Maybe this is not the best way to ascend directories, but it seems to
> work.

It's not working for windows. And using reduce to create a path instead of 

os.sep.join(aux)

seems to be showing off mad functional coding skillz than anything else...

Diez



More information about the Python-list mailing list