os.system issues

Chris Rebert clp2 at rebertia.com
Thu Feb 5 21:08:09 CET 2009


On Thu, Feb 5, 2009 at 6:00 AM, Youri Lammers
<youri_lammers_88 at hotmail.com> wrote:
> Ok,
>
> I want to run a program called 'muscle' with my python script,
> muscle uses the following command:
> 'muscle.exe -in filename -out filename'
> so far I got:
>
> import os
> args = ['-in filename', '-out filename']

As Christian indirectly points out, that's an incorrect tokenization
of the arguments. Remember that the shell has no knowledge of what
arguments a program takes and so doesn't treat the arguments
differently or specially for each program; it instead applies the
consistent rule of breaking arguments at spaces (unless you put an
argument in quotes). Thus, args should be:

args = ['-in', 'filename', '-out', 'filename']

The fact that the program happens to semantically pair those adjacent
arguments together is entirely up to and done by the program itself.
You can verify this by printing sys.argv from a command-line Python
program which you've given arguments to.

And of course, you'd only use `args` like this if you were using the
`subprocess` module; as others have pointed out, os.system() is more
primitive and just takes a single string of the entire command. But
`subprocess` is better anyway.

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com



More information about the Python-list mailing list