for循环内部for循环导致第二次循环失败

时间:2016-03-24 15:28:39

标签: python loops for-loop nested

晚上好,

我正在处理一个代码,该代码检查磁盘地址以查看是否存在磁盘。我通过执行扫描,禁用磁盘,再次扫描,然后比较结果以确定磁盘来验证它是哪个磁盘编号,然后在循环重复之前重新启用磁盘以扫描下一个地址。正如你在我的输出中看到的那样,它经历了这个过程,得到了第1个插槽的scan1和scan2,我已经打印出来以显示磁盘0已被删除,因此必须是插槽中的磁盘。循环重复下一个插槽并获取scan1和scan2以显示删除后,disk1已消失,这意味着它位于该插槽中。

但是,当我在第二个for循环内部比较两个字符串并实际将该差异保存到变量时,变量结果的输出只是一个空字符串' &#39 ;.因为它只是一个空白字符串,所以我得到字符串索引超出范围错误消息,这是有道理的。我只是不明白主for循环中的第二个for循环如何适用于一个循环,但是当第二次比较scan1和scan2时(即使我可以看到它们不同)只需将空字符串存储到结果中。

# Addresses of each populated slot
Slot1PackedAddress = '+-01.0-[82]----00.0'
Slot2PackedAddress = '+-03.0-[84]----00.0'
Slot3PackedAddress = '+-03.0-[08]----00.0'
Slot4PackedAddress = '+-01.0-[01]----00.0'
Addresses = [Slot1PackedAddress, Slot2PackedAddress, Slot3PackedAddress, Slot4PackedAddress]

InitialChecks = [None]*5
diskchange = [0]*5
Slot = [None]*5

SlotOn = ['4/1/', '5/1/', '6/1/', '7/1']
SlotOff = ['4/0/', '5/0/', '6/0/', '7/0']

for i in range(1,3):

    InitialChecks[i] = ['Slot%i = 1' %i]
    InitialChecks[i] = str(InitialChecks[i]).replace('[\'', '').replace('\']', '')

    with open('lspci.sh', 'rb') as file:
        script = file.read()
    subprocess.call(script, shell=True)

    if Addresses[i-1] in open('results').read():
        result = ' '

        print("Device in Slot %d, checking to see what drive number it is..." %i)

        scan1 = ''
        # Initial disk scan
        os.system("sudo parted -l > scan1.txt")
        with open('scan1.txt', 'rb') as file:
            for line in file:
                for part in line.split():
                    if "nvme" in part:
                        scan1 = scan1 + part
        print scan1

        # Disable the slot to get ready to record which drive number disappeared
        with open('removeslot%d.sh' %i, 'rb') as file:
            script = file.read()
        subprocess.call(script, shell=True)

        scan2 = ''
        # Initial disk scan
        os.system("sudo parted -l > scan2.txt")
        with open('scan2.txt', 'rb') as file:
            for line in file:
                for part in line.split():
                    if "nvme" in part:
                        scan2 = scan2 + part
        print scan2

        for nvme in scan1:
            if nvme not in scan2:
                    result = result + nvme 


        print("result is " + result)

        disk = filter(str.isdigit, result)
        strdiskchange = str(disk)
        diskchange[i] = int(strdiskchange[0])
        print diskchange[1]
        print("The new disk added to slot %i is /dev/nvme%dn1" %(i, diskchange[i]))

        # Rescan to re-enable the drive that was disabled.
        with open('rescan.sh', 'rb') as file:
            script = file.read()
        subprocess.call(script, shell=True)

        # Represents that Slot 1 is populated and on
        Slot[i] = 1

以下是错误输出:

Device in Slot 1, checking to see what drive number it is...
/dev/nvme0n1:/dev/nvme1n1:
/dev/nvme1n1:
drive that disappear is 0
The new disk added to slot 1 is /dev/nvme0n1
Device in Slot 2, checking to see what drive number it is...
/dev/nvme0n1:/dev/nvme1n1:
/dev/nvme0n1:
drive that disappear is 
Traceback (most recent call last):
  File "GUIArduino.py", line 75, in <module>
    diskchange[i] = int(strdiskchange[0])
IndexError: string index out of range
pciedev3ubuntu@PCIeDev3ubuntu:~/Documents$ 

感谢帮助人员

2 个答案:

答案 0 :(得分:0)

你可以使用tryCatch函数来忽略循环中的错误(例如空白或空单元格/向量)并继续它停止的地方

http://mazamascience.com/WorkingWithData/?p=912

答案 1 :(得分:0)

我明白了。我回去使用正在运行的lsblk代码,但刚刚添加了&#39;和&#39; p&#39;不排队&#39;部分跳过任何有p的行,因为只有分区的行包含p。

    lsblk = subprocess.Popen(['lsblk'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    scan2 = [line.strip() for line in lsblk.stdout if 'nvme' in line and 'p' not in line]

刚刚取代

    os.system("sudo parted -l > scan1.txt")
    with open('scan1.txt', 'rb') as file:
        for line in file:
            for part in line.split():
                if "nvme" in part:
                    scan1 = scan1 + part
相关问题