Piping data into script under Win32

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

    #1

    Piping data into script under Win32

    I trying to figure out a way to make a python script accept data output
    from another process under Windows XP, but I fail miserably. I have a
    vague memory having read this is not possible with Python under
    Windows...

    But googling for a clue I came across this from /Learning Python/ (Lutz
    & Ascher) [1]


    <Learning Python>

    import sys
    data = sys.stdin.readl ines()
    print "Counted", len(data), "lines."

    On Unix, you could test it by doing something like:


    % cat countlines.py | python countlines.py
    Counted 3 lines.

    On Windows or DOS, you'd do:


    C:\> type countlines.py | python countlines.py
    Counted 3 lines.

    </Learning Python>


    So: Is it possible to pipe data under DOS/Win9x but not NT/2k/XP or is
    it a bug in /Learning Python/ ?


    I've tried about all kinds of sane and insane approaches involving
    sys.stdin, but they all results in IOError: [Errno 9] Bad file
    descriptor.


    [1] <http://www.oreilly.com/catalog/lpython/chapter/ch09.html>

  • Jaime Wyant

    #2
    Re: Piping data into script under Win32

    I can't think of any reason that wouldn't work. You've entered the
    code in exactly like you have it below?

    Could you paste the traceback from your console?

    jw

    On 15 Apr 2005 19:11:44 -0700, runes <rune.strand@gm ail.com> wrote:[color=blue]
    > I trying to figure out a way to make a python script accept data output
    > from another process under Windows XP, but I fail miserably. I have a
    > vague memory having read this is not possible with Python under
    > Windows...
    >
    > But googling for a clue I came across this from /Learning Python/ (Lutz
    > & Ascher) [1]
    >
    > <Learning Python>
    >
    > import sys
    > data = sys.stdin.readl ines()
    > print "Counted", len(data), "lines."
    >
    > On Unix, you could test it by doing something like:
    >
    > % cat countlines.py | python countlines.py
    > Counted 3 lines.
    >
    > On Windows or DOS, you'd do:
    >
    > C:\> type countlines.py | python countlines.py
    > Counted 3 lines.
    >
    > </Learning Python>
    >
    > So: Is it possible to pipe data under DOS/Win9x but not NT/2k/XP or is
    > it a bug in /Learning Python/ ?
    >
    > I've tried about all kinds of sane and insane approaches involving
    > sys.stdin, but they all results in IOError: [Errno 9] Bad file
    > descriptor.
    >
    > [1] <http://www.oreilly.com/catalog/lpython/chapter/ch09.html>
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >[/color]

    Comment

    • R. C. James Harlow

      #3
      Re: Piping data into script under Win32

      On Saturday 16 April 2005 03:11, runes wrote:[color=blue]
      > I trying to figure out a way to make a python script accept data output
      > from another process under Windows XP, but I fail miserably. I have a
      > vague memory having read this is not possible with Python under
      > Windows...
      >
      > C:\> type countlines.py | python countlines.py
      > Counted 3 lines.[/color]

      Are you definitely doing this and not:

      C:\> type countlines.py | countlines.py

      That will give you the error you're seeing. The example you posted works for
      me.

      -----BEGIN PGP SIGNATURE-----
      Version: GnuPG v1.2.4 (GNU/Linux)

      iD8DBQBCYHpwY6W 16wIJgxQRArR9AJ 4vRbHQELs6d6BqU UDDNGBxAhWMhACg h4eR
      Sg8+vSF93a4RR7f gFcRNJDE=
      =3j2L
      -----END PGP SIGNATURE-----

      Comment

      • runes

        #4
        Re: Piping data into script under Win32

        You being so sure about what you were saying, was what I needed.
        Thanks!

        Under Windows, I'm used to rely on the PATHEXT env. variable, so I
        typically don't write "python scriptname.py args", but just "scriptname
        args".

        So:
        type countlines.py | python countlines.py = Success
        type countlines.py | countlines.py = Failure

        Why doesn't the latter work?

        Traceback:
        F:\groks>type countlines.py | countlines.py
        The process tried to write to a nonexistent pipe.
        Traceback (most recent call last):
        File "C:\groks\count lines.py", line 2, in ?
        data = sys.stdin.readl ines()
        IOError: [Errno 9] Bad file descriptor

        Comment

        • R. C. James Harlow

          #5
          Re: Piping data into script under Win32

          On Saturday 16 April 2005 03:43, runes wrote:
          [color=blue]
          > type countlines.py | python countlines.py = Success
          > type countlines.py | countlines.py = Failure
          >
          > Why doesn't the latter work?[/color]

          Don't quote me on this, but I think it's because invoking countlines.py
          involves running some sort of wrapper that discards its stdin and stdout.

          If anyone has a more authorative answer, I'd like to know, because this caught
          me out too.

          james.

          -----BEGIN PGP SIGNATURE-----
          Version: GnuPG v1.2.4 (GNU/Linux)

          iD8DBQBCYIVHY6W 16wIJgxQRAjScAJ 4umerCch3+aC8OR hJzNk3i7FHrwQCf cAxl
          /99yvz5++XsF5qf0 6rhdZic=
          =fAya
          -----END PGP SIGNATURE-----

          Comment

          • runes

            #6
            Re: Piping data into script under Win32

            Thanks James! I'll post the explanation if I find it ;-)

            Rune

            Comment

            Working...