473,799 Members | 2,683 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:
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


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
Mar 23 '06 #1
9 1839
gry

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("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:
>>> 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 >>>
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(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"])

Mar 23 '06 #2
Quoth gr*@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, do**@drizzle.co m
Mar 23 '06 #3
gr*@ll.mit.edu wrote:
<cut>

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


I got this output then:
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 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


It seems that it doesn't matter.

--
mph
Mar 23 '06 #4
Donn Cave wrote:
<cut>

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, do**@drizzle.co m


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']
%
Mar 23 '06 #5
Martin P. Hellwig <mh******@xs4al l.nl> wrote:
std_out, std_in = popen2.popen2(" F:\coding\pwSyn c\popen_test\te stia.py")

^^
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:
"F:\coding\pwSy nc\popen_test\t estia.py"

'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 -- si***@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
Mar 23 '06 #6
Sion Arrowsmith wrote:
"F:\coding\pwSy nc\popen_test\t estia.py" 'F:\\coding\\pw Sync\\popen_tes t\testia.py'


this might make it more obvious that something's not quite right with that
string literal:
print "F:\coding\pwSy nc\popen_test\t estia.py"

F:\coding\pwSyn c\popen_test estia.py

</F>

Mar 23 '06 #7
Sion Arrowsmith wrote:
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).


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
Mar 23 '06 #8
Kent Johnson <ke**@kentsjohn son.com> wrote:
Sion Arrowsmith wrote:
(and please avoid the abuse of raw strings for Windows paths).

Why do you consider that abuse of raw strings?


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:
os.listdir(r"C: \")

File "<stdin>", line 1
os.listdir(r"C: \")
^
SyntaxError: EOL while scanning single-quoted string

and getting all confused on this group.

--
\S -- si***@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
Mar 24 '06 #9
Sion Arrowsmith wrote:
Kent Johnson <ke**@kentsjohn son.com> wrote:
Sion Arrowsmith wrote:
(and please avoid the abuse of raw strings for Windows paths).


Why do you consider that abuse of raw strings?


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:
os.listdir( r"C:\")


File "<stdin>", line 1
os.listdir(r"C: \")
^
SyntaxError: EOL while scanning single-quoted string

and getting all confused on this group.


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
Mar 25 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2442
by: cherico | last post by:
from popen2 import popen2 r, w = popen2 ( 'tr "" ""' ) w.write ( t ) # t is a text file of around 30k bytes w.close () text = r.readlines () print text r.close () This simple script halted on
1
3112
by: | last post by:
This could possibly be a bug, but I don't understand it fully so I'm posting here first. Searching the list told me other people are having this problem too. I have created a class which inherits popen2.Popen3. The problem is that popen2.Popen3 does not define a __del__ method to handle proper closing of the pipes or destruction of the process. So I created one in my class:
4
1666
by: P | last post by:
I've written a couple of apps that required running a command and grabbing the output, and I've found the existing interfaces problematic for this. I think the proliferation of functions and classes in the popen2 module illustrates the problem (popen2.{popen2,popen3,popen4,Popen3,Popen4}) Now if I want to read both stdout and stderr seperately then it's awkward to say the least to implement that without deadlocking using
1
2663
by: Vivien Mallet | last post by:
Hello, I use popen2.Popen4 and I experienced problems with it. Let me show you with an example. The following script is called "lines" (it prints lines of 'X'): --------------------- #!/usr/bin/env python import sys
2
1612
by: Mage | last post by:
Hello, maybe my question is about linux and not about python. I tried to write a proxy script for "psql" command. I need some query log. I failed to read from the file object. I have tested: a,b = os.popen2('ls')
1
2583
by: Magnus Lycka | last post by:
I'm trying to read standard out in a process started with popen2 in a non-blocking way. (Other good ways of doing this than the one I tried are appreciated.) I've tried to dumb down my code to see what happens, and socket.poll seems to behave very strangely. I've tried to use the .poll method for the poll object with and without a timeout, but in either case, the output randomly switches between on of the versions below. It runs fast,...
1
1598
by: mikem76 | last post by:
Is there a way to get the process id when starting a process using os.popen2 or os.popen3 on linux? Mike
3
2092
by: Daniel Klein | last post by:
Here's a c routine that prints a single line : #include <stdio.h> main() { printf ("Hello World!\n"); } And now the Python program (called 'po.py') that uses 'popen2' :
5
2957
by: flupke | last post by:
Hi, i made a backup script to backup my postgres database. Problem is that it prompts for a password. It thought i could solve this by using popen2. I tested popen2 with dir (i'm on windows 2000, python 2.4.3) and it works. However when i try popen2 and my pg_dump command, it prompts for a password and I was under the impression that i was going to be able to dynamically communicate with the process.
0
9689
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9550
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10495
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10248
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10032
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7573
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5597
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.