don't understand popen2

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin P. Hellwig

    don't understand popen2

    Hi all,

    I was doing some popen2 tests so that I'm more comfortable using it.
    I wrote a little python script to help me test that (testia.py):

    ---------------------------------
    someline = raw_input("some thing:")

    if someline == 'test':
    print("yup")
    else:
    print("nope")
    ---------------------------------

    And another little thing that does it's popen2 stuff:

    ---------------------------------
    import popen2

    std_out, std_in = popen2.popen2(" testia.py")

    x=std_out.readl ine()
    print(x)

    std_in.writelin es("notgood")

    x=std_out.readl ine()
    print(x)
    ---------------------------------

    Now what I expected was that I got the return one the first line:
    "something: " and on the second "nope", but instead of that I got:
    [color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]
    something:
    Traceback (most recent call last):
    File "F:\coding\pwSy nc\popen_test\p open_test.py", line 8, in ?
    std_in.writelin es("notgood")
    IOError: [Errno 22] Invalid argument[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    I played around a bit with flush, write and the order of first writing
    and then reading, the best I can get is no error but still not the
    expected output. I googled a bit copied some examples that also worked
    on my machine, reread the manual and the only conclusion I have is that
    I don't even understand what I'm doing wrong.

    Would you please be so kind to explain my wrong doing?
    (python 2.4 + win32 extensions on XPProSP2)

    Thanks in advance!

    --
    mph
  • gry@ll.mit.edu

    #2
    Re: don't understand popen2


    Martin P. Hellwig wrote:[color=blue]
    > Hi all,
    >
    > I was doing some popen2 tests so that I'm more comfortable using it.
    > I wrote a little python script to help me test that (testia.py):
    >
    > ---------------------------------
    > someline = raw_input("some thing:")
    >
    > if someline == 'test':
    > print("yup")
    > else:
    > print("nope")
    > ---------------------------------
    >
    > And another little thing that does it's popen2 stuff:
    >
    > ---------------------------------
    > import popen2
    >
    > std_out, std_in = popen2.popen2(" testia.py")
    >
    > x=std_out.readl ine()
    > print(x)
    >
    > std_in.writelin es("notgood")
    >
    > x=std_out.readl ine()
    > print(x)
    > ---------------------------------
    >
    > Now what I expected was that I got the return one the first line:
    > "something: " and on the second "nope", but instead of that I got:
    >[color=green][color=darkred]
    > >>>[/color][/color]
    > something:
    > Traceback (most recent call last):
    > File "F:\coding\pwSy nc\popen_test\p open_test.py", line 8, in ?
    > std_in.writelin es("notgood")
    > IOError: [Errno 22] Invalid argument[color=green][color=darkred]
    > >>>[/color][/color]
    >
    > I played around a bit with flush, write and the order of first writing
    > and then reading, the best I can get is no error but still not the
    > expected output. I googled a bit copied some examples that also worked
    > on my machine, reread the manual and the only conclusion I have is that
    > I don't even understand what I'm doing wrong.
    >
    > Would you please be so kind to explain my wrong doing?
    > (python 2.4 + win32 extensions on XPProSP2)[/color]
    [color=blue][color=green][color=darkred]
    >>> help(sys.stdin. writelines)[/color][/color][/color]
    Help on built-in function writelines:

    writelines(...)
    writelines(sequ ence_of_strings ) -> None. Write the strings to the
    file.

    Note that newlines are not added. The sequence can be any iterable
    object
    producing strings. This is equivalent to calling write() for each
    string>

    You gave it a single string, not a list(sequence) of strings. Try
    something like:
    std_in.writelin es(["notgood"])

    Comment

    • Donn Cave

      #3
      Re: don't understand popen2

      Quoth gry@ll.mit.edu:
      | Martin P. Hellwig wrote:
      ....
      |> import popen2
      |>
      |> std_out, std_in = popen2.popen2(" testia.py")
      |>
      |> x=std_out.readl ine()
      |> print(x)
      |>
      |> std_in.writelin es("notgood")
      |>
      |> x=std_out.readl ine()
      |> print(x)
      ....
      |> Traceback (most recent call last):
      |> File "F:\coding\pwSy nc\popen_test\p open_test.py", line 8, in ?
      |> std_in.writelin es("notgood")
      |> IOError: [Errno 22] Invalid argument
      |> >>>
      ....

      | You gave it a single string, not a list(sequence) of strings. Try
      | something like:
      | std_in.writelin es(["notgood"])

      Did you try it? For me, writelines(stri ng) works fine. Since in
      Python, a string is in a sense a sequence of strings, this doesn't
      even really contradict the documentation -

      | ... The sequence can be any iterable object producing strings.

      Anyway, it seems unlikely he would get that INVARG error for this
      reason. That's an error from the host operating system, not the
      interpreter, and it mostly likely refers to the file descriptor.
      Since it works for me, I guess his problem is basically this:

      |> (python 2.4 + win32 extensions on XPProSP2)

      Donn Cave, donn@drizzle.co m

      Comment

      • Martin P. Hellwig

        #4
        Re: don't understand popen2

        gry@ll.mit.edu wrote:
        <cut>[color=blue]
        >
        > You gave it a single string, not a list(sequence) of strings. Try
        > something like:
        > std_in.writelin es(["notgood"])
        >[/color]

        I got this output then:[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]
        something:
        Traceback (most recent call last):
        File "F:\coding\pwSy nc\popen_test\p open_test.py", line 8, in ?
        std_in.writelin es(["notgood"])
        IOError: [Errno 22] Invalid argument[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]
        something:
        Traceback (most recent call last):
        File "F:\coding\pwSy nc\popen_test\p open_test.py", line 8, in ?
        std_in.write("n otgood")
        IOError: [Errno 22] Invalid argument[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]

        It seems that it doesn't matter.

        --
        mph

        Comment

        • Martin P. Hellwig

          #5
          Re: don't understand popen2

          Donn Cave wrote:
          <cut>[color=blue]
          >
          > Anyway, it seems unlikely he would get that INVARG error for this
          > reason. That's an error from the host operating system, not the
          > interpreter, and it mostly likely refers to the file descriptor.
          > Since it works for me, I guess his problem is basically this:
          >
          > |> (python 2.4 + win32 extensions on XPProSP2)
          >
          > Donn Cave, donn@drizzle.co m[/color]

          Thank you for your suggestion.

          I shuffled it a bit around and executed it on my BSD box and there
          indeed it works, if I use that exactly program (well not the path var of
          course) on NT it has no error but it still not gives the expected
          results. So I guess that pipes,std-in, std-out & std-err work that
          different on NT then on other POSIX systems.

          The lucky thing is that the code where I am exercising for must run on a
          BSD system so my immediately problem is dealt with, but I'm still
          curious how to get this working on NT.

          I posted my new code and the results under my sep.

          --
          mph

          popen_test.py------------------------------------
          #! /usr/local/bin/python
          import popen2

          std_out, std_in = popen2.popen2(" F:\coding\pwSyn c\popen_test\te stia.py")
          std_in.writelin es("test\n")
          std_in.flush()
          std_in.close()
          x=std_out.readl ines()
          print(x)
          -------------------------------------------------

          testia.py----------------------------------------
          #! /usr/local/bin/python
          someline = raw_input("some thing:")

          if someline == 'test':
          print("yup")
          else:
          print("nope")
          -------------------------------------------------


          results on NT:
          F:\coding\pwSyn c\popen_test>po pen_test.py
          []


          results on BSD:
          %./popen_test.py
          ['something:yup\ n']
          %

          Comment

          • Sion Arrowsmith

            #6
            Re: don't understand popen2

            Martin P. Hellwig <mhellwig@xs4al l.nl> wrote:[color=blue]
            >std_out, std_in = popen2.popen2(" F:\coding\pwSyn c\popen_test\te stia.py")[/color]
            ^^
            Your problem is, I suspect, nothing to do with popen2(), which is
            supported by the fact that the only thing other than OS different
            between this and your working BSD version is the path:[color=blue][color=green][color=darkred]
            >>> "F:\coding\pwSy nc\popen_test\t estia.py"[/color][/color][/color]
            'F:\\coding\\pw Sync\\popen_tes t\testia.py'

            Try:
            std_out, std_in = popen2.popen2(" F:/coding/pwSync/popen_test/testia.py")
            or:
            std_out, std_in = popen2.popen2(" F:\\coding\\pwS ync\\popen_test \\testia.py")
            (and please avoid the abuse of raw strings for Windows paths).

            --
            \S -- siona@chiark.gr eenend.org.uk -- http://www.chaos.org.uk/~sion/
            ___ | "Frankly I have no feelings towards penguins one way or the other"
            \X/ | -- Arthur C. Clarke
            her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump

            Comment

            • Fredrik Lundh

              #7
              Re: don't understand popen2

              Sion Arrowsmith wrote:
              [color=blue][color=green][color=darkred]
              >>>> "F:\coding\pwSy nc\popen_test\t estia.py"[/color][/color]
              > 'F:\\coding\\pw Sync\\popen_tes t\testia.py'[/color]

              this might make it more obvious that something's not quite right with that
              string literal:
              [color=blue][color=green][color=darkred]
              >>> print "F:\coding\pwSy nc\popen_test\t estia.py"[/color][/color][/color]
              F:\coding\pwSyn c\popen_test estia.py

              </F>



              Comment

              • Kent Johnson

                #8
                Re: don't understand popen2

                Sion Arrowsmith wrote:[color=blue]
                > Try:
                > std_out, std_in = popen2.popen2(" F:/coding/pwSync/popen_test/testia.py")
                > or:
                > std_out, std_in = popen2.popen2(" F:\\coding\\pwS ync\\popen_test \\testia.py")
                > (and please avoid the abuse of raw strings for Windows paths).[/color]

                Why do you consider that abuse of raw strings? It's very easy and
                convenient to copy a path from Windows Explorer and paste it directly
                into a raw string, no editing required.

                Kent

                Comment

                • Sion Arrowsmith

                  #9
                  Re: don't understand popen2

                  Kent Johnson <kent@kentsjohn son.com> wrote:[color=blue]
                  >Sion Arrowsmith wrote:[color=green]
                  >> (and please avoid the abuse of raw strings for Windows paths).[/color]
                  >Why do you consider that abuse of raw strings?[/color]

                  I consider it abuse because it's not what they were invented for.
                  I consider discouraging it to be a good thing in order to reduce
                  the chances of people being bitten by, for instance:
                  [color=blue][color=green][color=darkred]
                  >>> os.listdir(r"C: \")[/color][/color][/color]
                  File "<stdin>", line 1
                  os.listdir(r"C: \")
                  ^
                  SyntaxError: EOL while scanning single-quoted string

                  and getting all confused on this group.

                  --
                  \S -- siona@chiark.gr eenend.org.uk -- http://www.chaos.org.uk/~sion/
                  ___ | "Frankly I have no feelings towards penguins one way or the other"
                  \X/ | -- Arthur C. Clarke
                  her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump

                  Comment

                  • Kent Johnson

                    #10
                    Re: don't understand popen2

                    Sion Arrowsmith wrote:[color=blue]
                    > Kent Johnson <kent@kentsjohn son.com> wrote:
                    >[color=green]
                    >>Sion Arrowsmith wrote:
                    >>[color=darkred]
                    >>>(and please avoid the abuse of raw strings for Windows paths).[/color]
                    >>
                    >>Why do you consider that abuse of raw strings?[/color]
                    >
                    > I consider it abuse because it's not what they were invented for.
                    > I consider discouraging it to be a good thing in order to reduce
                    > the chances of people being bitten by, for instance:
                    >[color=green][color=darkred]
                    >>>>os.listdir( r"C:\")[/color][/color]
                    >
                    > File "<stdin>", line 1
                    > os.listdir(r"C: \")
                    > ^
                    > SyntaxError: EOL while scanning single-quoted string
                    >
                    > and getting all confused on this group.[/color]

                    Judging from the traffic here, people are far more often bitten by *not*
                    using raw strings for paths containing \ than they are by using them for
                    paths ending with \. So perhaps we should encourage raw strings for
                    Windows paths. ;)

                    Kent

                    Comment

                    Working...