Mailing List Archive

Using os.popen with other python scripts
I'm having trouble using os.popen within a script to capture the output of
another script.
For example:

Script 1:

print "this is a test"

Script 2:

import os
x = os.popen('script1.py').readlines()
print x

Script2's output is just an empty list. I'm working on an NT system, through a
dos window.
Does anyone know what could be causing this behavior?

Thanks,
TST

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
Using os.popen with other python scripts [ In reply to ]
It works for me: Python 1.5.1, Solaris 2.5. (output below)
There must be a problem with your setup.

Does script1.py work from the command line?
Have you tried it in two steps?
temp = os.popen('script1.py')
print temp
x = temp.readlines()
print x



> cat script1.py
#!/opt/bin/python
print "this is a test"
> python
Python 1.5.1 (#3, Mar 7 1999, 20:11:49) [GCC 2.7.2] on sunos5
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import os
>>> x = os.popen('script1.py').readlines()
>>> print x
['this is a test\012']
>>>

--
tbryan@zarlut.utexas.edu
Remove the z from this address to reply.
Stop spam! http://spam.abuse.net/spam/
Using os.popen with other python scripts [ In reply to ]
On Tue, May 04, 1999 at 04:32:55PM +0000, skott1870@my-dejanews.com wrote:
>
>
> I'm having trouble using os.popen within a script to capture the output of
> another script.
> For example:
>
> Script 1:
>
> print "this is a test"
>
> Script 2:
>
> import os
> x = os.popen('script1.py').readlines()
> print x
>
> Script2's output is just an empty list. I'm working on an NT system, through a
> dos window.
> Does anyone know what could be causing this behavior?
>
> Thanks,
> TST
>
> -----------== Posted via Deja News, The Discussion Network ==----------
> http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
>
> --
> http://www.python.org/mailman/listinfo/python-list

I'm not familiar with NT system. But, don't have to specify "python"
interpretor somewhere when you invoke script1? Perhaps, script1.py is
missing
#!/usr/local/bin/python
or script2.py needs
os.popen('python script1.py').readlines()

William
Using os.popen with other python scripts [ In reply to ]
On Tue, 4 May 1999 14:24:07 -0400, William Park <parkw@better.net> wrote:
>I'm not familiar with NT system. But, don't have to specify "python"
>interpretor somewhere when you invoke script1? Perhaps, script1.py is
>missing
> #!/usr/local/bin/python
>or script2.py needs
> os.popen('python script1.py').readlines()

Not if the proper associations are set up in the registry...
I run most of my python programs by typing "program.py".
(But it's getting to be a pain renaming them all to "program.py"
whenever I want to run them.)

Although, breaking it out into two statements wouldn't be a bad idea.
i.e.:
spam = os.popen('script1.py')
spam.readlines()

Say, is script1.py in your path? You might also check the value of the
current directory (os.curdir), and make sure script1.py is somewhere in
there.

Later,
Blake.
Using os.popen with other python scripts [ In reply to ]
In article <7gn7fo$fqs$1@nnrp1.dejanews.com> skott1870@my-dejanews.com writes:
> Script 1:
>
> print "this is a test"
>
> Script 2:
>
> import os
> x = os.popen('script1.py').readlines()
> print x

Have you tried:

x = os.popen('python script1.py').readlines()

--
Phil Hunt....philh@vision25.demon.co.uk
Using os.popen with other python scripts [ In reply to ]
Blake Winton wrote:

> On Tue, 4 May 1999 14:24:07 -0400, William Park <parkw@better.net>
> wrote:
> >I'm not familiar with NT system. But, don't have to specify "python"
> >interpretor somewhere when you invoke script1? Perhaps, script1.py is
> >missing
> > #!/usr/local/bin/python
> >or script2.py needs
> > os.popen('python script1.py').readlines()
>
> Not if the proper associations are set up in the registry...

Sorry Blake. While 'script1.py' works at the command line, file
redirection is broken that way, (at least on NT). He needs to use
os.popen('python script1.py').

- Gordon
Using os.popen with other python scripts [ In reply to ]
Blake Winton <bwinton@iname.com> wrote in
<slrn7iv1e5.892.bwinton@tor.dhs.org>:

>On Tue, 4 May 1999 14:24:07 -0400, William Park <parkw@better.net> wrote:
>>I'm not familiar with NT system. But, don't have to specify "python"
>>interpretor somewhere when you invoke script1? Perhaps, script1.py is
>>missing
>> #!/usr/local/bin/python
>>or script2.py needs
>> os.popen('python script1.py').readlines()
>
>Not if the proper associations are set up in the registry...
>I run most of my python programs by typing "program.py".
>(But it's getting to be a pain renaming them all to "program.py"
> whenever I want to run them.)
>
>Although, breaking it out into two statements wouldn't be a bad idea.
>i.e.:
>spam = os.popen('script1.py')
>spam.readlines()
>
>Say, is script1.py in your path? You might also check the value of the
>current directory (os.curdir), and make sure script1.py is somewhere in
>there.

I think the problem here is the same bug that hits you under NT when you
try to redirect a python script from the command line. So while the command
'script1.py' (or 'script1' if you have set PATHEXT correctly) will print
"this is a test", running 'script1.py >afile' will not write any output in
the file, you have to use 'python script1.py >afile'.

So although the os.popen is running the script, you will lose all output.

The solution is to include 'python ' in the command as in:
import os
x = os.popen('d:\progra~1\python\python script1.py').readlines()
print x


--
Duncan Booth duncan@dales.rmplc.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
http://dales.rmplc.co.uk/Duncan
Using os.popen with other python scripts [ In reply to ]
In article <8DBD65246duncanrcpcouk@news.rmplc.co.uk>,
Duncan Booth <duncan@rcp.co.uk> wrote

> I think the problem here is the same bug that hits you under NT when you
> try to redirect a python script from the command line. So while the command
> 'script1.py' (or 'script1' if you have set PATHEXT correctly) will print
> "this is a test", running 'script1.py >afile' will not write any output in
> the file, you have to use 'python script1.py >afile'.
>
> So although the os.popen is running the script, you will lose all output.
>
> The solution is to include 'python ' in the command as in:
> import os
> x = os.popen('d:\progra~1\python\python script1.py').readlines()
> print x

That is indeed what the problem was, thank you very much.
TST

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own