Skip to content Skip to sidebar Skip to footer

Is There A Way To Determine If A Subdirectory Is In The Same Filesystem From Python When Using Os.walk?

I'm writing a python script that uses os.walk() to walk a directory tree. I'd like to give it the ability to skip subdirectories that are mounted to a different file system, the w

Solution 1:

Solution 2:

I think you can use a combination of the os.stat call and a filtering of the dirnames given by os.walk to do what you want. Something like this:

import osfor root, dirs, files inos.walk(somerootdir) :
    do_processing(root, dirs, files)
    dirs = [i for i in dirs ifos.stat(os.path.join(root, i)).st_dev == os.stat(root).st_dev]

That should modify the list of directories to recurse into, by removing those which do not have the same device.

I have no idea on how it will work on OS X, but it seems to be working here in Linux, after a very little bit of testing.

Post a Comment for "Is There A Way To Determine If A Subdirectory Is In The Same Filesystem From Python When Using Os.walk?"