mpc und list index out of range

Python auf Einplatinencomputer wie Raspberry Pi, Banana Pi / Python für Micro-Controller
Antworten
Tellan_42
User
Beiträge: 1
Registriert: Montag 27. Februar 2023, 17:15

hey, ich versuche gerade mein raspberry Zero als Mpd Server mit Display werkeln.
Soweit funktioniert alles.
Das Problem ist nun beim Abspielen von einem Radio Stream. Bei Werbung Ect kommt immer eine Fehlermeldung.
Leider habe ich das Problem mit meinen Begrenzten Python Fähigkeiten noch nicht im Griff bekommen.

Code: Alles auswählen

while True:
    if mpc_status() == 0:
        #os.system('/home/pi/scripts/mpd_display.py')
        # Display Data
        lcd_string(datetime.now().strftime('%m/%d/%Y %H:%M'),LCD_LINE_1,2)
        lcd_string(ip_address(),LCD_LINE_2,2)
        #lcd_string("CPU: "+measure_temp()+"c "+cpu_load()+"%",LCD_LINE_3,2)
        #lcd_string(" ",LCD_LINE_3,1)
        time.sleep(1)
    elif mpc_status() == 1:
        if (len(current_artist()) == 0):
            sstring = os.popen('mpc -f %title%').readline()
            astring = os.popen('mpc -f %title%').readline()
            song = sstring.split('-')[1]
            artist = astring.split('-')[0]
            lcd_string(artist.strip(),LCD_LINE_1,2)
            lcd_string(song.strip(),LCD_LINE_2,2)
            lcd_string(song_elapsed()+" / Stream Time",LCD_LINE_4,3)
        else:
            lcd_string(current_artist(),LCD_LINE_1,1)
            lcd_string(current_album(),LCD_LINE_2,1)
            lcd_string(current_song(),LCD_LINE_3,1)
            lcd_string(song_elapsed()+"/"+current_time(),LCD_LINE_4,3)

        time.sleep(0.1)
Fehlermeldung:

Code: Alles auswählen

pi@raspberrypi:~/progs $ python display_mit_mpd.py 

Traceback (most recent call last):
  File "/home/pi/progs/display_mit_mpd.py", line 239, in <module>
    main()
  File "/home/pi/progs/display_mit_mpd.py", line 222, in main
    song = sstring.split('-')[1]
    IndexError: list index out of range

Danke schonmal im vorraus
Benutzeravatar
__blackjack__
User
Beiträge: 13116
Registriert: Samstag 2. Juni 2018, 10:21
Wohnort: 127.0.0.1
Kontaktdaten:

@Tellan_42: Na da ist dann ganz offensichtlich kein "-" enthalten.

Es macht auch wenig Sinn Aufrufe mehrfach zu machen, bei denen das gleiche heraus kommt. Zudem sollte man `subprocess.run()` statt `os.popen()` verwenden.
„All religions are the same: religion is basically guilt, with different holidays.” — Cathy Ladman
Antworten