how to pass scripts to python -c

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Kramer

    how to pass scripts to python -c

    what are the formatting rules to passing python commands to the python
    command line? I've tried the following, which works:

    echo hello | python -c "import sys; print sys.stdin.read( )[:4]"

    I'm actually trying to shell out of another scripting lang that's not
    very good at string parsing to have python do some work.. the only
    problem is this other lang doesn't like the ";" in my python command
    string and fails. Is there another notation I can use on a single
    line to tell python that there is a line break?

    I tried:
    echo hello | python -c "import sys\n print sys.stdin.read( )[:4]"

    but that doesn't work

    any suggestions?

    thanks

    daniel
  • Guillermo Fernandez Castellanos

    #2
    Re: how to pass scripts to python -c

    As I understood your question, you want to pass information to your python
    program from your command line.

    Like:
    python pycat.py < file.txt
    Or:
    python pycat.py file1.txt file2.txt
    Or:
    cat file1 file2 | python pycat.py
    Or:
    python pycat.py
    (in this last case you write what you want and must push Crt-D to make what you
    wrote echoed to the terminal, and you can start again).

    You can do this with the library fileinput.

    This is a sample code that works and do what you expect from the last examples.
    It's a very simple cat program:

    #### pycat.py ####
    import fileinput
    if __name__=='__ma in__':
    for line in fileinput.input ():
    print line,

    Regards,

    Guille

    Daniel Kramer wrote:[color=blue]
    > what are the formatting rules to passing python commands to the python
    > command line? I've tried the following, which works:
    >
    > echo hello | python -c "import sys; print sys.stdin.read( )[:4]"
    >
    > I'm actually trying to shell out of another scripting lang that's not
    > very good at string parsing to have python do some work.. the only
    > problem is this other lang doesn't like the ";" in my python command
    > string and fails. Is there another notation I can use on a single
    > line to tell python that there is a line break?
    >
    > I tried:
    > echo hello | python -c "import sys\n print sys.stdin.read( )[:4]"
    >
    > but that doesn't work
    >
    > any suggestions?
    >
    > thanks
    >
    > daniel[/color]

    Comment

    • Guillermo Fernandez Castellanos

      #3
      Re: how to pass scripts to python -c

      Oh! I found another version:

      #### pycat.py ####
      #!/usr/bin/env python
      import sys
      if len(sys.argv)== 1:
      while(1):
      readed=sys.stdi n.read(1)
      sys.stdout.writ e(readed)
      else:
      for files in sys.argv[1:]:
      filed=file(file s,'r')
      readed=filed.re ad()
      sys.stdout.writ e(readed)


      Don't ask me why you must put read(1) and not simply read(), it works, and I'm
      happy :-)
      But if someone could explain me why it works, it would be cool.

      Regards,

      Guille

      Daniel Kramer wrote:[color=blue]
      > what are the formatting rules to passing python commands to the python
      > command line? I've tried the following, which works:
      >
      > echo hello | python -c "import sys; print sys.stdin.read( )[:4]"
      >
      > I'm actually trying to shell out of another scripting lang that's not
      > very good at string parsing to have python do some work.. the only
      > problem is this other lang doesn't like the ";" in my python command
      > string and fails. Is there another notation I can use on a single
      > line to tell python that there is a line break?
      >
      > I tried:
      > echo hello | python -c "import sys\n print sys.stdin.read( )[:4]"
      >
      > but that doesn't work
      >
      > any suggestions?
      >
      > thanks
      >
      > daniel[/color]

      Comment

      Working...