-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.

6
  • 1
    Please be very specific about what you have tried. Commented May 29, 2014 at 2:34
  • 1
    subprocess is very relevant to the question. Commented May 29, 2014 at 2:37
  • And please be very specific about how the answers to this question and your previous question do not solve your problem. It seems you have left out quite a lot of detail. Commented May 29, 2014 at 3: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("<whatever command to be executed>") Commented May 29, 2014 at 7: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. Commented May 29, 2014 at 7:39

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

3 Comments

This is actually incorrect; you would pass separate arguments in the list, not one long string.
You're right; edited it.
All the more surprising your answer was marked as accepted before..
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)

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.