Wie interpretieren status-code in Python-Befehle.getstatusoutput()

In einem stellt sich die Frage,, fragte ich, wo finden Sie die Dokumentation für die C-Funktion "warten Sie." Dies war ein Versuch, um herauszufinden, return-codes für die Befehle.getstatusoutput () - Modul. Stackoverflow kam durch, aber die Dokumentation nicht helfen. Hier ist, was mich verwirrt:

#!/usr/bin/python
import commands
goodcommand = 'ls /'
badcommand = 'ls /fail'
status, output = commands.getstatusoutput(goodcommand)
print('Good command reported status of %s' % status)
status, output = commands.getstatusoutput(badcommand)
print('Bad command reported status of %s' % status)

Beim laufen auf OS X (Leopard) bekomme ich folgende Ausgabe: (die Spiele der Dokumentation).

$ python waitest.py 
Good command reported status of 0
Bad command reported status of 256

Unter OS X ist dabei ein "ls /fail ; echo $?" erhält man folgende Ausgabe:

$ ls /fail ; echo $?
ls: /fail: No such file or directory
1

Beim laufen auf Linux (Ubuntu Hardy) bekomme ich folgende Ausgabe:

$ python waitest.py 
Good command reported status of 0
Bad command reported status of 512

Unter Ubuntu tun, "ls /nicht bestanden" wird ein 2:

$ ls /fail ; echo $?
ls: cannot access /fail: No such file or directory
2

Also Python zu sein scheint Multiplikation status-codes durch 256. Huh? Ist das irgendwo dokumentiert?

Antwort von @Schof beantwortet die Frage "wenn Sie commands.getstatusoutput() warum sind die exitcodes, multipliziert mit 256?" direkt und mit Beispiel-code. Die beiden anderen Antworten, die mehr oder weniger sagen "verwenden subprocess statt commands.getstatusoutput()" oder "hier ist, wie zu verwenden Teilprozess".

InformationsquelleAutor Schof | 2009-10-08

Schreibe einen Kommentar