-7

I want to run a programme located in C, which can be run using command line using a line as follows

tclsh oommf.tcl avf2odt -average space -axis z -onefile  <filename>.txt  -headers none -ipat *.omf

This is the only thing I need to type in command line.

How can I run it using python?

Please be VERY relevant to the question. I have seen a lot of answers like: you can use 'subprocess' to realize it. However, none of them really solves the problem.

shuttle87
  • 14,785
  • 9
  • 69
  • 104
littleHui
  • 1
  • 2
  • 1
    Please be very specific about what you have tried. – merlin2011 May 29 '14 at 02:34
  • 1
    `subprocess` is very relevant to the question. – Martin Konecny May 29 '14 at 02:37
  • And please be very specific about how the answers to this question and [your previous question](http://stackoverflow.com/questions/23907698/how-to-run-a-tcl-script-in-a-folder-in-python) do not solve your problem. It seems you have left out quite a lot of detail. – beaker May 29 '14 at 03:19
  • Hi all, thanks for your suggestions(and some critics) To specify my problem, what I wish to achieve is using only python to run a command, as written in the code above, which can be run within command line. If this is still not specified enough, I shall list out all the procedure: 1 open command line 2 type in the line I wrote above 3 press Enter to execute That's all Now I want to achieve in Python. I have searched an solution by myself, as below import os os.system("") – littleHui May 29 '14 at 07:39
  • I am new to programming and that's why I need detailed explanation on 'subprocess', despite that I do know it can solve the problem. I even don't know what 'shell' is and you can imagine I may not be capable to describe the question in a skilled programmer's tone. – littleHui May 29 '14 at 07:39
  • Have you tried using the methods proposed in the answers? Did they give you the results you expected? – beaker May 29 '14 at 16:56

2 Answers2

1

You can use subprocess module.

import subprocess
subprocess.call(["tclsh", "oommf.tcl", "avf2odt", "-average", "space", "-axis", "z", "-onefile",  "<filename>.txt", "-headers", "none", "-ipat", "*.omf"])

See https://docs.python.org/2/library/subprocess.html#subprocess.call

Charles Clayton
  • 13,212
  • 10
  • 73
  • 114
0

This should do the trick (returns the process's printed output as a String)

import subprocess
output = subprocess.check_output("tclsh oommf.tcl avf2odt -average space -axis z -onefile  <filename>.txt  -headers none -ipat *.omf", shell=True)

If you want the subprocess's return code instead (for example to check for 0 on success) use

import subprocess
return_code = subprocess.call("tclsh oommf.tcl avf2odt -average space -axis z -onefile  <filename>.txt  -headers none -ipat *.omf", shell=True)
Martin Konecny
  • 50,691
  • 18
  • 119
  • 145
  • Don't use `shell=True` where splitting up the arguments into a list of separate strings lets you avoid using the extra shell process altogether. – Martijn Pieters Jun 05 '14 at 08:43