1

I have a slight problem. I have a software which has a command with two inputs. The command is: maf2hal inputfile outputfile.

I need to call this command from a Python script. The Python script asks the user for path of input file and path of output file and stores them in two variables.The problem is that when I call the command maf2hal giving the two variable names as the arguments, the error I get is cannot locate file.

Is there a way around this? Here's my code:

folderfound = "n" # looping condition
while (folderfound == "n"):
    path = raw_input("Enter path of file to convert (with the extension) > ")
    if not os.path.exists(path):
        print "\tERROR! file not found. Maybe file doesn't exist or no extension was provided. Try again!\n"
    else:
        print "\tFile found\n"
        folderfound = "y"

folderfound = "y" # looping condition
while (folderfound == "y"):
    outName = raw_input("Enter path of output file to be created > ")
    if os.path.exists(outName):
        print "\tERROR! File already exists \n\tEither delete the existing file or enter a new file name\n\n"
    else:
        print "Creating output file....\n"
        outputName = outName + ".maf"
        print "Done\n"
        folderfound = "n"
hal_input = outputName #inputfilename, 1st argument
hal_output = outName + ".hal" #outputfilename, 2nd argument

call("maf2hal hal_input hal_output", shell=True)
2
  • try printing the varaibles "hal_input" and "hal_output". It could be an issue with "/" and relative paths Commented Jan 9, 2015 at 14:35
  • I tried printing too and it was fine! Commented Jan 9, 2015 at 14:36

4 Answers 4

6

This is wrong:

call("maf2hal hal_input hal_output", shell=True)

It should be:

call(["maf2hal", hal_input, hal_output])

Otherwise you're giving "hal_input" as the actual file name, rather than using the variable.

You should not use shell=True unless absolutely necessary, and in this case it is not only unnecessary, it is pointlessly inefficient. Just call the executable directly, as above.

For bonus points, use check_call() instead of call(), because the former will actually check the return value and raise an exception if the program failed. Using call() doesn't, so errors may go unnoticed.

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

Comments

4

There are a few problems. Your first reported error was that the call to the shell can't find the maf2hal program - that sounds like a path issue. You need to verify that the command is in the path of the shell that is being created.

Second, your call is passing the words "hal_input" and "hal_output". You'll need to build that command up first to pass the values of those variables;

cmd = "maf2hal {0} {1}".format(hal_input, hal_output)
call(cmd, shell=True)

Comments

3

Your code is literally trying to open a file called hal_input, not using the contents of your variable with the same name. It looks like you're using the subprocess module to execute, so you can just change it to call(["maf2hal", hal_input, hal_output], shell=True) to use the contents.

Comments

2

at the end of your code:

call("maf2hal hal_input hal_output", shell=True)

you are literally calling that string, not the executable and then those paths, you need to concatenate your strings together first, either by adding them of using .join
eg:

call("maf2hal " + hal_input + " " + hal_output", shell=True)

or

call("maf2hal ".join(hal_input, " ", hal_output), shell=True)

2 Comments

While true, I would not recommend constructing the command string in this way. Using format() or passing the arguments and program in a list as suggested above is a lot more readable.
i don't use the subprocess module much, i had forgotten you could pass it a list as the first argument.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.