Using os.popen with other python scripts

Duncan Booth duncan at rcp.co.uk
Wed May 5 04:55:59 EDT 1999


Blake Winton <bwinton at iname.com> wrote in 
<slrn7iv1e5.892.bwinton at tor.dhs.org>:

>On Tue, 4 May 1999 14:24:07 -0400, William Park <parkw at 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 at 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




More information about the Python-list mailing list