Python | os.popen

  • Heyho, bin noch neu im Forum, hab aber mal ne Frage zu os.popen.

    Warum ergibt dieses script :


    Code
    import os
    import time
    f=os.popen("mpc current") 
    station = "" 
    for i in f.readlines(): 
      station += i 
    os.system(station)

    das aus ----> sh: 1: N-Joy: not found

    ABER dieses 'Script'

    Code
    import os
    os.system("mpc current")

    das aus ----> N-Joy

    Die Frage deshalb weil ich das später auf ein LCD schreiben lassen will. Es wird die 'aktueller Sender' Funktion eines Webradios....

    Ich blick des nicht...

  • also wenn os.system aus dem aufruf des befehls mpc current den wert NJOY zurückliert (2 script)
    dann versucht er er im ersten script den befehl "was auch immer in station steht" auszuführen -> not found. Und readlines() brauchst du nicht zusammenbauen, er liest alles auf einmal ein.

    Code
    station = i

    Der Unterschied zwischen Genie und Wahnsinn definiert sich im Erfolg.

    Edited once, last by dbv (June 20, 2013 at 8:45 PM).

  • Ah ok, danke für die schnelle antwort. Ich habs mal mit 'print()' probiert, jetzt gehts.

    Code
    import os
    import time
    f=os.popen("mpc current") 
    
    
    for i in f.readlines(): 
      station = i
    print("Es leauft gerade "+station)

    gibt jetz[font="Arial"]t 'Es läuft gerade Njoy' aus.[/font]

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!