Bonjour,
Je souhaiterais comprendre la différence entre commands.getoutput() et os.popen(). Je n'arrive pas à la voir !
merci d'avance :)
Edité par Guigui_: merci d'utiliser un titre clair
Version imprimable
Bonjour,
Je souhaiterais comprendre la différence entre commands.getoutput() et os.popen(). Je n'arrive pas à la voir !
merci d'avance :)
Edité par Guigui_: merci d'utiliser un titre clair
Apparemment, personne ne peut m'aider! :oops:
commands.getoutput() :
Exemple :Citation:
getstatusoutput( cmd)
Execute the string cmd in a shell with os.popen() and return a 2-tuple (status, output). cmd is actually run as { cmd ; } 2>&1, so that the returned output will contain output or error messages. A trailing newline is stripped from the output. The exit status for the command can be interpreted according to the rules for the C function wait().
getoutput( cmd)
Like getstatusoutput(), except the exit status is ignored and the return value is a string containing the command's output.
et os.popen():Code:
1
2
3
4
5
6
7 >>> import commands >>> commands.getstatusoutput('ls /bin/ls') (0, '/bin/ls') >>> commands.getstatusoutput('/bin/junk') (256, 'sh: /bin/junk: not found') >>> commands.getoutput('ls /bin/ls') '/bin/ls'
si ça peut t'aider!!Citation:
popen( command[, mode[, bufsize]])
Open a pipe to or from command. The return value is an open file object connected to the pipe, which can be read or written depending on whether mode is 'r' (default) or 'w'. The bufsize argument has the same meaning as the corresponding argument to the built-in open() function. The exit status of the command (encoded in the format specified for wait()) is available as the return value of the close() method of the file object, except that when the exit status is zero (termination without errors), None is returned. Availability: Macintosh, Unix, Windows.
Changed in version 2.0: This function worked unreliably under Windows in earlier versions of Python. This was due to the use of the _popen() function from the libraries provided with Windows. Newer versions of Python do not use the broken implementation from the Windows libraries.
Pour moi il n'y a pas de grande différence
commands.getoutput(cmd) execute la cmmande sans te renvoyer l'exit code
os.popen(cmd) : Execute la commande et si tu fais :
variable contiendra l'exit codeCode:variable = os.popen("c:\windows\notepad.exe")
Merci j'avais dû mal à voir la différence!