【发布时间】:2023-03-15 16:00:01
【问题描述】:
我为 LIRC 创建了一个蛮力 IR blaster,但我循环通过命令的循环不断抛出索引失败,任何想法:
在第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*******"
【问题讨论】:
-
请将您的示例简化为minimal reproducible example,并在您的问题中包含回溯。
-
我们不知道第48行是什么(我不会数);
for coms in comfile:行肯定不会产生IndexError。 -
您有一个
print(values[0])“调试”电话。为什么不为coms和comvalues添加类似的变量,然后再使用这些变量? -
对不起,不知道是否没有列出行号。
-
发布有关产生异常的代码的问题时,请始终包含完整的 Traceback - 复制并粘贴它,然后将其格式化为代码(选择并输入
ctrl-k).. Catch the error 并检查/print 除外套件中的相关数据。 ..comand = comvalues[1]-comvalues必须少于 2 个项目/事物。
标签: python loops index-error