[Tutor] Equivalent of && in Python?

Eric Brunson brunson at brunson.com
Mon Aug 27 17:29:04 CEST 2007

wormwood_3 wrote:
> I have a script that reads some local system information, performs some calculations, and then launches some terminal windows:
> # Spawn the 4 terminals, with needed positions and sizes, then exit
> commands.getoutput("%s --geometry=%dx%d+%d+%d --working-directory=%s" % \
>     (terminal, t1width, t1height, t1posx, t1posy, workingdir))
> commands.getoutput("%s --geometry=%dx%d+%d+%d --working-directory=%s" % \
>     (terminal, t2width, t2height, t2posx, t2posy, workingdir))
> commands.getoutput("%s --geometry=%dx%d+%d+%d --working-directory=%s" % \
>     (terminal, t3width, t3height, t3posx, t3posy, workingdir))
> commands.getoutput("%s --geometry=%dx%d+%d+%d --working-directory=%s" % \
>     (terminal, t4width, t4height, t4posx, t4posy, workingdir))
> The oddity: When I call this script, sometimes all four terminals launch, one right after another, which is the desired behaviour. 

That is what's actually odd.  Just reading the script without being well 
versed in the intricacies of the command module, I would expect them to 
be run sequentially.

> At other times, one will launch, and ONLY after I close it will the second launch, and so on until the fourth. I do not understand how this is happening. I thought each line in a script which does anything has to be done before the next one is executed, but I may be way off on this. 
> If this were in a bash script, I could add " &&" after each line, but what to do in a Python script?

Actually, I think you mean a single ampersand.  "&&" is condition 
execution of the next command and is done synchronously.

I would say try putting an ampersand after the command in getoutput(), 
have you tried that?  If that doesn't work, then make sure getoutput() 
is using a shell, since the shell is the mechanism through which the 
ampersand works, or else read the docs for getoutput() to see if there's 
some way to get it in the background.  If none of those solutions work, 
you could spawn a thread for each command, or finally, use the 
subprocess module, which I know can be instructed to use a subshell.

> -Sam
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

More information about the Tutor mailing list