Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prevent Python's os.walk from walking across mount points?

Tags:

python

linux

unix

In Unix all disks are exposed as paths in the main filesystem, so os.walk('/') would traverse, for example, /media/cdrom as well as the primary hard disk, and that is undesirable for some applications.

How do I get an os.walk that stays on a single device?

Related:

  • Is there a way to determine if a subdirectory is in the same filesystem from python when using os.walk?
like image 347
joeforker Avatar asked Feb 23 '09 14:02

joeforker


2 Answers

From os.walk docs:

When topdown is true, the caller can modify the dirnames list in-place (perhaps using del or slice assignment), and walk() will only recurse into the subdirectories whose names remain in dirnames; this can be used to prune the search

So something like this should work:

for root, dirnames, filenames in os.walk(...):
  dirnames[:] = [
    dir for dir in dirnames
    if not os.path.ismount(os.path.join(root, dir))]
  ...
like image 53
Constantin Avatar answered Oct 13 '22 01:10

Constantin


I think os.path.ismount might work for you. You code might look something like this:

import os
import os.path
for root, dirs, files in os.walk('/'):
    # Handle files.
    dirs[:] = filter(lambda dir: not os.path.ismount(os.path.join(root, dir)), 
                  dirs)

You may also find this answer helpful in building your solution.

*Thanks for the comments on filtering dirs correctly.

like image 39
zweiterlinde Avatar answered Oct 13 '22 01:10

zweiterlinde