Reputation: 509
I am attempting to run a command in terminal through a Python script. The command, which only takes three arguments, works perfectly when performed in terminal. Here is what is entered in the command line:
gpmetis inputfile numberOfSections
Wherever the inputfile
is from, say the Desktop, the outputfile
is dumped in the same location after gpmetis
is executed (it is named inputfile.part.numberOfSections
). gpmetis
only works in terminal, but for condensing purposes, I want to use it during the middle of the Python script to save time. (I would previously just shuffle around files to and from terminal and Python)
But this is where I run into problems... this question and this forum gave helpful hints on how to execute terminal within Python, but I'm still not receiving the outputfile
when I run the python code. It's like the output is suppressed or the way I call terminal is faulty.
I'm currently calling terminal like:
def Terminal(inputfile, NumParts):
os.system("gpmetis inputfile NumParts")
outputfile = "inputfile.part." + NumParts
return outputfile
And I don't get an error from this, but I don't receive any output file either. What am I missing here and if you know could you explain it? I'm trying to learn Python, so describing what I'm screwing up would be much appreciated.
os
has been imported. There may be a fault with how I am "returning" the outputfile
in my script, but I also do not see an outputfile
on my desktop, which is the first problem to be dealt with (one step at a time!)
NOTE: I found documentation that is related, but will it help me? I'm having trouble understanding it..
Upvotes: 0
Views: 3859
Reputation: 468
First, you might check the return value of os.system
. A non-zero return value usually indicates that an error has occurred.
It looks like you are trying to use the parameters in your command. In that case it should look like:
def Terminal(inputfile, NumParts):
command = 'gpmetis ' + inputfile + ' ' + str(NumParts)
os.system(command)
outputfile = intputfile + '.part.' + str(NumParts)
return outputfile
Lastly, based on this answer, it looks like you may be better off using the subprocess
module. You can then execute the same command like this:
import subprocess
subprocess.call(['gpmetis', inputfile, str(NumParts)])
Upvotes: 4
Reputation: 281
Here inputfile
is a variable passed to the function, but in the commandline argument, it is taken as the filename.
You may want to try
os.system('gpmetis ' + str(inputfile) + ' ' + str(NumParts))
outputfile = intputfile + '.part.' + str(NumParts)
It is hard to say without the calling code and the intent. Let me know if it helps.
Upvotes: 0