don't understand popen2

gry at ll.mit.edu gry at ll.mit.edu
Wed Mar 22 21:35:14 EST 2006


Martin P. Hellwig wrote:
> 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("something:")
>
> 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.readline()
> print(x)
>
> std_in.writelines("notgood")
>
> x=std_out.readline()
> 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:
>
>  >>>
> something:
> Traceback (most recent call last):
>    File "F:\coding\pwSync\popen_test\popen_test.py", line 8, in ?
>      std_in.writelines("notgood")
> IOError: [Errno 22] Invalid argument
>  >>>
>
> 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)

>>> help(sys.stdin.writelines)
Help on built-in function writelines:

writelines(...)
    writelines(sequence_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.writelines(["notgood"])




More information about the Python-list mailing list