[Tutor] Request for help with os.walk() combining os.path.ismount() in Linux

Srihari Vijayaraghavan linux.bug.reporting at gmail.com
Thu Aug 20 05:03:57 CEST 2015


Hello Folks,

Please consider the following 2 scripts:
1. os_path_ismount.py:
import os
import sys
out = sys.stdout.write
out("%s\n" % os.path.ismount(sys.argv[1]))

2. os_walk.py:
import sys
import os
out = sys.stdout.write
for root, dirs, files in os.walk("/"):
   out("The dirs before removing mount points: %s\n" % dirs)
   for d in dirs:
       dname = os.path.join(root, d)
       if os.path.ismount(dname):
           dirs.remove(d)
   out("The dirs after removing mount points: %s\n" % dirs)
   sys.exit()

Am at a loss to understand this quirky behaviour on Fedora 22 (x86-64;
both stock-standard python 2.7.10 & 3.4.2):
[srihari at laptop ~]$ python2 ./os_path_ismount.py /proc
True

[srihari at laptop ~]$ python3 ./os_path_ismount.py /proc
True

[srihari at laptop ~]$ python2 ./os_walk.py
The dirs before removing mount points: ['run', 'dev', 'srv', 'fedora',
'root', 'bin', 'lib', 'opt', 'lost+found', 'etc', 'sbin', 'var',
'sys', 'media', 'backup', 'home', 'usr', 'tmp', 'proc', 'mnt', 'boot',
'lib64']
The dirs after removing mount points: ['dev', 'srv', 'fedora', 'root',
'bin', 'lib', 'opt', 'lost+found', 'etc', 'sbin', 'var', 'media',
'backup', 'usr', 'proc', 'mnt', 'lib64']

[srihari at laptop ~]$ python3 ./os_walk.py
The dirs before removing mount points: ['run', 'dev', 'srv', 'fedora',
'root', 'bin', 'lib', 'opt', 'lost+found', 'etc', 'sbin', 'var',
'sys', 'media', 'backup', 'home', 'usr', 'tmp', 'proc', 'mnt', 'boot',
'lib64']
The dirs after removing mount points: ['dev', 'srv', 'fedora', 'root',
'bin', 'lib', 'opt', 'lost+found', 'etc', 'sbin', 'var', 'media',
'backup', 'usr', 'proc', 'mnt', 'lib64']

Undoubtedly proc (to be precise /proc) (same for /dev as well) is
indeed a mount point, yet it's somehow not being evicted. So am I
doing something silly? Could somebody please explain what am I doing
wrong here?

However, /run & /sys are being evicted, which is good, of course.

(Interestingly, I get different results on different platforms:
CentOS6.7 gives some results, CentOS7.1 something else, i.e., am
totally baffled!)

Thank you.
Srihari Vijayaraghavan

PS: Of course, I could add an exclusion list to take out the "known"
mount points, but that won't be elegant I reckon. Certainly, it won't
be for my real usage scenario.


More information about the Tutor mailing list