8

I am using Unix OS and I want to know how to execute a command that is executable from a different software in Python script. I have this software installed into my system called HAL Tools and it has a command called maf2hal with two arguments which are input and output files. The path for the command is saved in my .bash_profile under PATH variable. I call the command from UNIX likewise:

[root ~]$ maf2hal inputfile outputfile

I want to call this command from a Python script. What is the statement or function that I need to use.

glglgl
  • 89,107
  • 13
  • 149
  • 217

2 Answers2

3

1.Use os.popen() to execute command:

import os
os.popen("maf2hal inputfile outputfile")

2.subprocess.call()

from subprocess import call
call("maf2hal inputfile outputfile", shell=True)
Paul Lo
  • 6,032
  • 6
  • 31
  • 36
  • Don't suggest to use `os.popen()`. `Your 2nd option, `subprocess`, is *the* way to go nowadays. – glglgl Jan 08 '15 at 15:38
  • @glglgl Thanks for the feedback, I also found there was a good summary on previous post here: http://stackoverflow.com/questions/89228/calling-an-external-command-in-python – Paul Lo Jan 08 '15 at 15:42
  • @varshith-chakrapani also check this previous post: http://stackoverflow.com/questions/89228/calling-an-external-command-in-python – Paul Lo Jan 08 '15 at 15:42
  • 2
    Yeah I checked the previous discussion. Seems that subprocess indeed is better suited. Thanks! – Varshith Chakrapani Jan 08 '15 at 15:56
2

You can have a look at os.system

A sample command can be

import os
os.system('maf2hal inputfile outputfile')
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
  • I have no idea why this was downvoted... – That1Guy Jan 08 '15 at 17:47
  • @That1Guy Perhaps, the other answer is better ..... But still :( ..Thanks for +1 – Bhargav Rao Jan 08 '15 at 17:59
  • "The `subprocess` module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function." This answer is not wrong, but inferior to other ways of doing it. – glglgl Jan 09 '15 at 13:14
  • @glglgl. yeah this is an inferior alternative. (Thats why the downvote I guess), but thanks for re-affirming that this answer is not wrong ...:)... – Bhargav Rao Jan 09 '15 at 13:16