IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Python Discussion :

Différence entre commands.getoutput() et os.popen()


Sujet :

Python

  1. #1
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Février 2006
    Messages
    27
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2006
    Messages : 27
    Points : 28
    Points
    28
    Par défaut Différence entre commands.getoutput() et os.popen()
    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

  2. #2
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Février 2006
    Messages
    27
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2006
    Messages : 27
    Points : 28
    Points
    28
    Par défaut
    Apparemment, personne ne peut m'aider!

  3. #3
    Membre habitué
    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    192
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France

    Informations forums :
    Inscription : Octobre 2004
    Messages : 192
    Points : 160
    Points
    160
    Par défaut
    commands.getoutput() :


    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.
    Exemple :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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'
    et os.popen():

    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.
    si ça peut t'aider!!

    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 :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    variable = os.popen("c:\windows\notepad.exe")
    variable contiendra l'exit code

  4. #4
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Février 2006
    Messages
    27
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2006
    Messages : 27
    Points : 28
    Points
    28
    Par défaut
    Merci j'avais dû mal à voir la différence!

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Différence entre deux commandes del
    Par yohann64 dans le forum Scripts/Batch
    Réponses: 3
    Dernier message: 16/12/2014, 17h40
  2. Réponses: 0
    Dernier message: 17/11/2014, 10h29
  3. Différence entre deux commande basées sur git push
    Par poussinvert dans le forum GIT
    Réponses: 5
    Dernier message: 22/05/2014, 17h35
  4. Réponses: 4
    Dernier message: 19/02/2014, 14h39
  5. Différence entre commands et command
    Par jo385 dans le forum Général VBA
    Réponses: 3
    Dernier message: 02/07/2008, 10h54

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo