pythondirectory-walk

Walking directories


I am searching for a specific directory (/android).

I know that in python I can walk directories with os.walk(root_dir) but the problem here is that I do not know if the directory that I am looking for is a child of root_dir or if its a parent directory of root_dir.

Is there any method that perform the same operation than os.walk() but in the opposite way?

Thanks.


Solution

  • You can go to parent directory of root_dir by using .. in os.path.abspath().

    import os
    parent_dir = os.path.abspath(os.path.join(root_dir, ".."))
    

    Now you have parent_directory of root_dir in parent_dir, You can make it root_dir and use os.walk(root_dir) again.