using python with -c (as a inline execution in shell)

Discussion in 'Python' started by les ander, Nov 16, 2004.

  1. les ander

    les ander Guest

    Hi,
    in perl one can do all sorts of stuff in a single line form the command shell.
    python has the -c option.
    it is easy to do stuff like,
    python -c "print 10+20"
    etc statements
    but suppose I want to read from the stdin (piped) i don't know how to do this since
    python -c "from sys import stdin; for x in stdin: print x"

    gives a syntax error.

    Anyone one know how to do this?
    Also, is there is site that talkes about doing this kind of stuff?
    the man pages in xterm are pretty sparse and don't say that much
    thanks
    les ander, Nov 16, 2004
    #1
    1. Advertising

  2. les> python -c "from sys import stdin; for x in stdin: print x"

    les> gives a syntax error.

    les> Anyone one know how to do this?

    How about:

    % python -c 'from sys import stdin
    > for x in stdin:
    > print x
    > '

    hi there
    bye
    hi there

    bye

    Skip
    Skip Montanaro, Nov 16, 2004
    #2
    1. Advertising

  3. On 16 Nov 2004 07:44:19 -0800, les ander <> wrote:
    > but suppose I want to read from the stdin (piped) i don't know how to do this since
    > python -c "from sys import stdin; for x in stdin: print x"
    >
    > gives a syntax error.


    If you really must do this, there's always PyOne:
    <http://www.unixuser.org/~euske/pyone/>.

    Wouldn't touch it with a bargepole, myself.

    --
    Cheers,
    Simon B,
    ,
    http://www.brunningonline.net/simon/blog/
    Simon Brunning, Nov 16, 2004
    #3
  4. les ander <> wrote:

    > but suppose I want to read from the stdin (piped) i don't know how to do
    > this since python -c "from sys import stdin; for x in stdin: print x"
    >
    > gives a syntax error.
    >
    > Anyone one know how to do this?


    Not _good_ ways, but:

    import sys; print sys.stdin.read()

    import sys; sys.stdout.write(sys.stdin.read())

    import sys; sys.stdout.writelines(sys.stdin)

    are some approaches. None matches the double-spacing effect you appear
    to be after, but changing every '\n' into two ain't _that_ hard, e.g.

    import sys; print sys.stdin.read().replace('\n','\n\n')

    etc, will give kinda the same doublespacing your approach would give if
    it worked. Still, Python just isn't oneline-oriented...


    Alex
    Alex Martelli, Nov 16, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christian Heimes
    Replies:
    0
    Views:
    601
    Christian Heimes
    Feb 27, 2008
  2. Gerardo Herzig
    Replies:
    1
    Views:
    1,079
    Philipp Pagel
    Feb 27, 2008
  3. Samuel A. Falvo II
    Replies:
    0
    Views:
    484
    Samuel A. Falvo II
    Oct 8, 2008
  4. J.B. Brown
    Replies:
    1
    Views:
    550
    Neil Hodgson
    Jul 23, 2010
  5. MRAB
    Replies:
    0
    Views:
    421
Loading...

Share This Page