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 :

Exécuter du code python dans un string (python -c cmd)


Sujet :

Python

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Août 2004
    Messages
    69
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2004
    Messages : 69
    Points : 63
    Points
    63
    Par défaut Exécuter du code python dans un string (python -c cmd)
    Bonjour,

    J'ai une question bête. Comment exécuter du code python dans un string en appelant le code python avec l'argument "-c" ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    python -c 'for i in range(5): print i; print i+10'
    Output :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    0
    10
    1
    11
    2
    12
    3
    13
    4
    14
    Supposez que je veuille exécuter ce code en une ligne :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    for i in range(5):
        print i
        print i+10
     
    print "end"
    La ligne suivante ne marche évidemment pas :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    python -c 'for i in range(5): print i; print i+10'; print "end"
    Output :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
     
    0
    10
    end
    1
    11
    end
    2
    12
    end
    3
    13
    end
    4
    14
    end
    Alors que j'aurais voulu juste un print "end" à la fin.

    Merci d'avance,

    piloupy

  2. #2
    Expert éminent sénior
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 338
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : Architecte technique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2008
    Messages : 21 338
    Points : 36 860
    Points
    36 860
    Par défaut
    Salut,
    Le manuel dit:
    -c <command>¶

    Execute the Python code in command. command can be one or more statements separated by newlines, with significant leading whitespace as in normal module code.

    If this option is given, the first element of sys.argv will be "-c" and the current directory will be added to the start of sys.path (allowing modules in that directory to be imported as top level modules).
    Donc...
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    python -c "
    > for i in range(3):
    >     print i
    >     print i + 10
    > print 'end'"
    0
    10
    1
    11
    2
    12
    end
    Cool
    - W

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Août 2004
    Messages
    69
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2004
    Messages : 69
    Points : 63
    Points
    63
    Par défaut
    Merci wiztricks.

    C'était en effet tout bête ^^;

    J'avais pas trouvé la page de doc sur le python command-line.

    En tout cas, je sors d'un gros projet Perl, et j'avoue que mes premiers pas en python me plaisent beaucoup !

    Quand on a fait du Perl, on sait pourquoi Python est bien...

    piloupy

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

Discussions similaires

  1. Réponses: 5
    Dernier message: 19/01/2011, 13h47
  2. Exécuter du code PHP dans un onchange javascript
    Par beegees dans le forum Général JavaScript
    Réponses: 4
    Dernier message: 18/01/2009, 17h08
  3. Réponses: 7
    Dernier message: 11/03/2007, 09h35
  4. Interpreter un script python dans un prog python
    Par romeo9423 dans le forum Général Python
    Réponses: 3
    Dernier message: 01/12/2005, 16h16

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