[Tutor] Help with os.system, os.popen and fellows

Charlie Clark charlie at begeistert.org
Mon Sep 15 12:40:36 EDT 2003


Dear list,

I've got a problem trying to work out how Python works with the shell using 
system, popen and exec...

I'm trying to write a script to replace a shell script. I need to open a 
program and send it messages via a command line application and 
occasionally read responses. Although the programs themselves are BeOS 
specific I'm hoping that the way of handling them isn't. BeOS is fairly 
POSIX compatible as long as sockets aren't involved.

Here's an extract from the initial script (for bash 2.04)
	/boot/apps/Becasso/Becasso -S3 &
	sleep 2
	hey -s Becasso set ExportFormat to image/jpeg

for x in *.jpg
do
	hey -s Becasso create Canvas with Name=spam and Size=BRect[100,100,899,699]

So I need to start "Becasso" and keep it running. I've found I've only been 
able to do this effectively using os.popen although I think os.fork might 
be more appropriate. Most of the other commands are fairly straight forward 
as "hey" does the actual work returning values so I think os.popen should 
be correct here. The problem I've hit is that os.popen commands don't seem 
to return control to the Python interpreter.

Here's what might script currently looks like:
import os

files = os.listdir(os.curdir)

os.popen("/boot/apps/Becasso/Becasso -S3")
cmd = "hey -s Becasso "
r = os.popen(cmd + "set ExportFormat to image/jpeg")

for file in files:
	r = os.popen(cmd + "create Canvas with Name=spam \
		and Size=BRect[100,100,899,699]").read()
	r = os.popen(cmd + "set Contents of Layer 0 of Canvas 'spam' to file\
		%s").read() %file
	
	ratio = os.popen("hey -o Becasso get AspectRatio of Canvas 0").read()

This seems to get stuck on the second os.popen.

Thanx for any pointers.

Charlie



More information about the Tutor mailing list