简体   繁体   中英

How to check volume is mounted or not using python with a dynamic volume name

import subprocess

def volumeCheck(volume_name):
    """This function will check volume name is mounted or not.
    """
volume_name = raw_input('Enter volume name:')
volumeCheck(volume_name)

print 'volume_name=',volume_name    
p = subprocess.Popen(['df', '-h'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p1, err = p.communicate()
pattern = p1

if pattern.find(volume_name):
    print 'volume found'
else:
    print 'volume not found'

While running i always got wrong result "volume found".

root@sr-query:/# df -h
Filesystem            Size  Used Avail Use% Mounted on
rootfs                938M  473M  418M  54% /
/dev/md0              938M  473M  418M  54% /
none                  250M  4.9M  245M   2% /dev
/dev/md2              9.7M  1.2M  8.0M  13% /usr/config
/dev/md7              961M   18M  895M   2% /downloads
tmpfs                 250M  7.9M  242M   4% /var/volatile
tmpfs                 250M     0  250M   0% /dev/shm
tmpfs                 250M     0  250M   0% /media/ram
**/dev/mapper/vg9-lv9  1016M   65M  901M   7% /VolumeData/sp
/dev/mapper/vg10-lv10
                     1016M   65M  901M   7% /VolumeData/cp**
root@sr-query:/# 
root@sr-query:/# 

root@sr-query:/# python volume_check.py

Enter volume name:raid_10volume

volume_name= raid_10volume
**volume found**
root@sr-query:/# 

I enterd raid_10volume its not listed here please check the df -h command out put(only 2 volume there sp and cp) , then how it reached else part. what is wrong in my code? Thanks in advance.

is there any other way to do this work ! ?

pattern is a string, so the find method returns -1 when the pattern is not found. You need to change your comparison line to:

if pattern.find(volume_name) != -1:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM