Python 外观失败,我不知道为什么:索引错误:列出索引超出范围



我为 LIRC 创建了一个蛮力 IR 发射器,但我循环命令的循环不断抛出索引失败,任何想法:

它位于第 48 行,comfile 循环中的 coms:

IndexError: list index out of range

我不知道为什么它不会循环然后转到下一个文件。

import os
import shutil
import subprocess
import time
# Using readline() 
count = 0
#os.remove("list.list")
os.system("touch /home/pi/com.list")
os.system("touch /home/pi/list.list")
os.system("systemctl start lircd")
for filename in os.listdir('confs'):
currconf = "/home/pi/confs/" + filename
print "**********start*******"
print "1. ", filename
#print "2. ", currconf
#os.system("systemctl stop lircd")
#subprocess.call('systemctl stop lircd', shell=True)
shutil.move(currconf, "/etc/lirc/lircd.conf")

subprocess.call('systemctl reset-failed lircd', shell=True)
subprocess.call('systemctl restart lircd', shell=True)
time.sleep(1)
subprocess.call('systemctl status lircd | tail -3', shell=True)
# os.system("systemctl start lircd")
# irlist  = ""
#print "3. ", irlist
os.remove("/home/pi/list.list")
os.remove("/home/pi/com.list")
os.system('irsend list "" "" >> /home/pi/list.list')

qbfile = open("/home/pi/list.list", "r")
for aline in qbfile:
values = aline.split()  
print(values[0])
rname = values[0].strip('n')
print "2. rname", rname
comlist = 'irsend list ' + rname + ' "" >> /home/pi/com.list'
print "3. comlist", comlist
os.system(comlist)
comfile = open("/home/pi/com.list", "r")
for coms in comfile:
comvalues = coms.split()  
comand = comvalues[1]#.strip('n')
cmd =  "irsend SEND_ONCE " + rname + " " + comand
print "4. cmd ", cmd
time.sleep(.001)
os.system(cmd)
print "**********end*******"

添加了

If comsvalue:

这过滤掉了空值并让它继续。

最新更新