0

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

Community
  • 1
  • 1
Ason
  • 509
  • 2
  • 9
  • 25

2 Answers2

4

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)])
Community
  • 1
  • 1
Thomas Fussell
  • 438
  • 3
  • 8
  • awesome! That worked perfectly. Is there a distinction between when I should use `os.system` or `subprocess`? – Ason Jul 11 '12 at 23:46
  • 1
    Yes. You should always use `subprocess` -- the documentation for `os.system` even suggests this. – kindall Jul 12 '12 at 00:05
  • `subprocess` was added in Python 2.4 to replace older modules/functions including os.system. Calls using `os.system` are a bit more succinct at the cost of reduced functionality. `subprocess` allows better error handling and more options (e.g piping), particularly if you use the full `subprocess.Popen` object. You can read more about it here: http://docs.python.org/library/subprocess.html – Thomas Fussell Jul 12 '12 at 00:12
  • Thank you guys, I appreciate the help and explanations! Conceptually I was confused when previously reading that documentation, but I'll have another go at it. – Ason Jul 12 '12 at 17:20
0

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.

Sudhir Krishnan
  • 281
  • 3
  • 7
  • `outputfile` still isnt showing up. I noticed that you put `' '` in between the two strings, should it go between `'gpmetis'` and `str(inputfile)` as well? – Ason Jul 11 '12 at 23:40
  • I was trying to get inputfile variable and form the command by concatenating the command and arguments. Then I pass the command to os.system() call. – Sudhir Krishnan Jul 11 '12 at 23:44
  • ok.. thanks for explaining! It still didn't work, but that makes sense. – Ason Jul 11 '12 at 23:45