import subprocess as sub
DriveMounted = "grep -qs \"linuxLUN01\" /proc/mounts"
if sub.call(DriveMounted, shell=True):
print "drive is mounted"
else:
print "drive is not mounted"
import subprocess as sub
p = sub.Popen(['grep', '-qs', 'linuxLUN01', '/proc/mounts'], stdout=sub.PIPE)
p.wait() # waits for the command to finish running
mounted = bool(p.stdout.read()) # see if grep produced any output
print "drive is", "mounted" if mounted else "not mounted"
使用
subprocess.call()
代替:subprocess.Popen
只返回Popen
实例,而不是它应该执行的命令的返回值。在subprocess.call(...)
是subprocess.Popen(...).wait()
的一个简单方便函数。在sub.Popen(DriveMounted, shell=True)
构造一个Popen
对象,它将始终是True
这实际上并没有运行命令。你可能想要更像这样的东西:相关问题 更多 >
编程相关推荐