python中的字符串匹配不起作用

时间:2012-10-18 12:25:36

标签: python python-3.x python-2.7

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) 

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

new_vol = '/VolumeData/' '+ volume_name +'

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

    print 'volume mounted'

else:
    print 'volume not mounted'

new_vol ='/ VolumeData /''+ volume_name +'我认为这是错误的,但我该怎么办呢?

我想搜索\ volumeData \ volume_name。

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:/# 

感谢adavnce

1 个答案:

答案 0 :(得分:1)

你究竟想要什么?如果我理解得当,我可能会尝试os.path.join,例如:

new_vol = os.path.join('/','VolumeData', volume_name)

此外,由于您实际上并未使用find的结果,因此您可能需要考虑:

if new_vol in p1:

代替。