Systemd 启动时如何修复“IndexError:列表索引超出范围”?

问题描述 投票:0回答:1

我正在尝试解决这个问题:

 crawl_oled.service - test OLED SSD1306
     Loaded: loaded (/lib/systemd/system/crawl_oled.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Wed 2024-03-06 08:30:14 EST; 2min 33s ago
    Process: 200235 ExecStartPre=/bin/sleep 30 (code=exited, status=0/SUCCESS)
    Process: 200600 ExecStart=/usr/bin/python3 /home/pi/luma-env/luma.examples/examples/OLED_INFO.py (code=exited, status=1/FAILURE)
   Main PID: 200600 (code=exited, status=1/FAILURE)
        CPU: 609ms

Mar 06 08:30:14 raspberrypi python3[200600]: Traceback (most recent call last):
Mar 06 08:30:14 raspberrypi python3[200600]:   File "/home/pi/luma-env/luma.examples/examples/OLED_INFO.py", line 299, in <module>
Mar 06 08:30:14 raspberrypi python3[200600]:     run_ext()
Mar 06 08:30:14 raspberrypi python3[200600]:   File "/home/pi/luma-env/luma.examples/examples/OLED_INFO.py", line 59, in run_ext
Mar 06 08:30:14 raspberrypi python3[200600]:     random_py = random.choice(Local_List)
Mar 06 08:30:14 raspberrypi python3[200600]:   File "/usr/lib/python3.9/random.py", line 347, in choice
Mar 06 08:30:14 raspberrypi python3[200600]:     return seq[self._randbelow(len(seq))]
Mar 06 08:30:14 raspberrypi python3[200600]: IndexError: list index out of range
Mar 06 08:30:14 raspberrypi systemd[1]: crawl_oled.service: Main process exited, code=exited, status=1/FAILURE`
Mar 06 08:30:14 raspberrypi systemd[1]: crawl_oled.service: Failed with result 'exit-code'.

...

pi@raspberrypi:~ $ cat /etc/os-release  PRETTY_NAME="Debian GNU/Linux 11 (bullseye)" NAME="Debian GNU/Linux" VERSION_ID="11" VERSION="11 (bullseye)" VERSION_CODENAME=bullseye ID=debian HOME_URL="https://www.debian.org/" SUPPORT_URL="https://www.debian.org/support" BUG_REPORT_URL="https://bugs.debian.org/"

该脚本过去在启动时正常运行,但自从我添加了

random
函数以随机从 Luma Examples 启动脚本后,该脚本就失败了。

这是脚本中失败的部分

def run_ext():
    i = 0
    while True:
        try:
            # Command to run "OTHER".py
            ####random_file=random.choice(os.listdir())
            Local_List = [each for i, each in enumerate(os.listdir(current_working_directory)) if each.endswith('1.py')]
            random_py = random.choice(Local_List)
            random_py = current_working_directory + '/' + str(random_py)
            print(random_py)
            sp.run(random_py, timeout=10)
        except sp.TimeoutExpired:
            break 

将 i 引入

random_py = random.choice(Local_List[i])
时,出现以下错误:

crawl_oled.service - test OLED SSD1306
     Loaded: loaded (/lib/systemd/system/crawl_oled.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Wed 2024-03-06 08:52:57 EST; 2s ago
    Process: 216809 ExecStartPre=/bin/sleep 30 (code=exited, status=0/SUCCESS)
    Process: 217172 ExecStart=/usr/bin/python3 /home/pi/luma-env/luma.examples/examples/OLED_INFO.py (code=exited, status=1/FAILURE)
   Main PID: 217172 (code=exited, status=1/FAILURE)
        CPU: 545ms

Mar 06 08:52:56 raspberrypi systemd[1]: Started test OLED SSD1306.
Mar 06 08:52:57 raspberrypi python3[217172]: /
Mar 06 08:52:57 raspberrypi python3[217172]: Traceback (most recent call last):
Mar 06 08:52:57 raspberrypi python3[217172]:   File "/home/pi/luma-env/luma.examples/examples/OLED_INFO.py", line 299, in <module>
Mar 06 08:52:57 raspberrypi python3[217172]:     run_ext()
Mar 06 08:52:57 raspberrypi python3[217172]:   File "/home/pi/luma-env/luma.examples/examples/OLED_INFO.py", line 59, in run_ext
Mar 06 08:52:57 raspberrypi python3[217172]:     random_py = random.choice(Local_List[i])
Mar 06 08:52:57 raspberrypi python3[217172]: IndexError: list index out of range
Mar 06 08:52:57 raspberrypi systemd[1]: crawl_oled.service: Main process exited, code=exited, status=1/FAILURE
Mar 06 08:52:57 raspberrypi systemd[1]: crawl_oled.service: Failed with result 'exit-code'.

有人可以教我如何解决这个问题以及让这个脚本按预期工作的最佳方法是什么: 从 Luma Examples 文件夹中随机选择一个示例并运行 x 秒,然后继续执行 main() 的其余部分。 值得注意的是,当从 Geany 吃午餐时,这个相同的脚本可以正常运行,不会出现错误。

python raspberry-pi
1个回答
0
投票

错误消息表明列表为空。

为了调试此问题,可能只需

print
引发异常的行之前的列表即可。

此外,填充列表的代码相当晦涩且复杂。您可能想用类似的东西替换它

from pathlib import Path

...
        cwdobj = Path(current_working_directory)
        local_files = list(cwdobj.glob("*1.py"))
        # fail if there are no matches
        assert local_files
        random_py = random.choice(local_files)
        sp.run(str(random_py), timeout=10)

旧版

glob
库具有大致相似的功能,但我认为您会欣赏
pathlib.Path
如何封装目录条目,您可以查询其基本名称、文件存在等,以及将其转换为完整路径只需将其转换回字符串,就像这里一样。

(顺便说一句,如果

current_working_directory
正如它所说,这个变量完全是多余的。操作系统始终知道您所在的目录。也许还可以参见当前工作目录到底是什么?。)

© www.soinside.com 2019 - 2024. All rights reserved.