I have a python application running on a beaglebone. How do I (in Python) check if the "/mnt" partition is mounted as read-only or read-write?
EDIT: The answer makes the assumption that you plan to write to /mnt.
I would just try to write to it and catch OSError exception to handle the read-only case.
This should do the trick for you:
def isMountReadonly(mnt):
with open('/proc/mounts') as f:
for line in f:
device, mount_point, filesystem, flags, __, __ = line.split()
flags = flags.split(',')
if mount_point == mnt:
return 'ro' in flags
raise ValueError('mount "%s" doesn\'t exist' % mnt)
print "read only: %s" % isMountReadonly('/mnt')
Output:
read only: False
Solution is pretty much trivial, with only 1 syscall (statvfs).
stat = os.statvfs('/mnt')
# Python < 3.2
ST_RDONLY = 1
readonly = bool(stat.f_flag & ST_RDONLY)
# Python >= 3.2
readonly = bool(stat.f_flag & os.ST_RDONLY)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.