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

Vue hybride

Message pr�c�dent Message pr�c�dent   Message suivant Message suivant
  1. #1
    Membre confirm�
    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
    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
    Homme Profil pro
    Architecte technique retrait�
    Inscrit en
    Juin 2008
    Messages
    21 754
    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 754
    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
    Architectures post-modernes.
    Python sur DVP c'est aussi des FAQs, des cours et tutoriels

  3. #3
    Membre confirm�
    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
    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