Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python For循环内部For循环导致第二个循环后失败_Python_Loops_For Loop_Nested - Fatal编程技术网

Python For循环内部For循环导致第二个循环后失败

Python For循环内部For循环导致第二个循环后失败,python,loops,for-loop,nested,Python,Loops,For Loop,Nested,大家晚上好 我正在编写一个检查磁盘地址的代码,以查看是否存在磁盘。我通过执行扫描、禁用磁盘、再次扫描来验证它是哪个磁盘号,然后比较结果以确定磁盘,然后在循环重复扫描下一个地址之前重新启用磁盘。正如您在下面的输出中所看到的,它经历了一个过程,获取插槽1的scan1和scan2,我打印了它以显示磁盘0已被移除,因此该磁盘必须是插槽中的磁盘。循环在下一个插槽中重复,并获取scan1和scan2,以显示在删除后,disk1已消失,这意味着它位于该插槽中 但是,当我在内部执行第二个for循环以比较这两个字

大家晚上好

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

但是,当我在内部执行第二个for循环以比较这两个字符串并实际将差异保存到变量时,变量结果的输出只是一个空白字符串“”。因为它只是一个空白字符串,所以我得到了字符串索引超出范围的错误消息,这是有意义的。我只是不明白为什么主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$ 
插槽1中的设备,正在检查驱动器号。。。
/dev/nvme0n1:/dev/nvme1n1:
/开发/nvme1n1:
消失的驱动器为0
添加到插槽1的新磁盘是/dev/nvme0n1
插槽2中的设备,正在检查驱动器号。。。
/dev/nvme0n1:/dev/nvme1n1:
/开发人员/nvme0n1:
消失的驱动器是
回溯(最近一次呼叫最后一次):
文件“guiardino.py”,第75行,在
diskchange[i]=int(strdiskchange[0])
索引器错误:字符串索引超出范围
pciedev3ubuntu@PCIeDev3ubuntu:~/Documents$

感谢大家的帮助

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


我想出来了。我继续使用lsblk代码,但只是添加了'and'p'not in line'部分,以跳过任何具有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

查看它在哪里显示消失的驱动器是。。。您有一个空字符串,因此
strdiskchange[0]
是一个超出我发布的范围的索引,我知道这一点,这就是我收到错误消息的原因。但是,通过字符串的第一个循环不是空的,正如你所看到的,我比较的两个字符串的差异是不同的,所以从我所知道的来看,它不应该是空的,所以这就是我试图解决的问题,不是错误消息本身。对于scan1中的nvme,此
scan1
字符串中迭代单个字符。我不清楚您期望的
结果
变量是什么。我认为您的
scan1
scan2
应该是列表或集合,而不是字符串。@PM2Ring,我相信我曾尝试过列表,并收到类似的结果。是否有任何特定的变量我应该设置在列表中/设置为?因为当我这样做的时候,我只是扫描了=['']*3。