[英]Python - Check if linux partition is read-only or read-write?
I have a python application running on a beaglebone. 我有一个在beaglebone上运行的python应用程序。 How do I (in Python) check if the "/mnt" partition is mounted as read-only or read-write?
如何(在Python中)检查“ / mnt”分区是否以只读或读写方式安装?
EDIT: The answer makes the assumption that you plan to write to /mnt. 编辑:答案做出了您计划写入/ mnt的假设。
I would just try to write to it and catch OSError exception to handle the read-only case. 我只是尝试写它并捕获OSError异常来处理只读情况。
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). 解决方案非常简单,只有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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.