Parameters for a Python executable

Asked

Viewed 453 times

1

I would like to know how to pass parameters to an executable file on python.

i have a python script that will call an executable and I need to pass two parameters to this executable.

I can call the executable with the os.system or subprocess*, what I’m not getting is to pass the parameters, for example with the subprocess_call it calls the executable but waits to pass the parameters, despite passing the parameters in the syntax.

An example of what I’ve tried:

#!/usr/bin/python

import subprocess

subprocess.call(["dgTQ0126L028.exe","TQ0126L028.0000","/scratchout/teste_python"])

He even calls the executable dgTQ0126L028.exe, but does not use the parameters I am passing TQ0126L028.0000 and /scratchout/teste_python

  • See if this helps https://docs.python.org/dev/library/argparse.html

  • python .exe ?

  • Indeed, you had not explained it correctly. I voted to reopen the question. Even so, I suggest you edit it and put the code you already have (because it will serve as a basis for someone to give you an answer). If possible make a [mcve] that illustrates your difficulty.

  • 1

    Read: http://answall.com/help/formatting

  • 1

    Are you sure it’s not a problem in the dgTQ0126L028.exe? (I voted to reopen)

  • Hello, I believe that the problem is not in the executable, because the program calls exec. but keeps waiting for the parameters and when I manually pass the parameters it does correctly

  • What do you mean by "the program calls exec"? Which program, the such dbg*. exe? Did you put a print inside this program to verify that the parameters are actually being received? If you invoke via the command line, are the parameters processed? I agree with @Guilhermenascimento because the problem may not be in your Python code.

  • If you run on CMD like this c:\pasta> dgTQ0126L028.exe TQ0126L028.0000 /scratchout/teste_python what happens?

  • When I run via command line the program runs correctly. I know it’s calling the executable when I use os.system and even subprocess.check_call, because it’s waiting for the two parameters that are needed ( TQ0126L028.0000 and /scratchout/teste_python). Calling the executable is when I invoke the executable.

  • correct is subprocess.check_call, test with another . exe, check these bars are not breaking the . exe

  • 1

    I ran a test here (I created a simple .exe in C# that prints what you get from the command line) and I couldn’t reproduce the problem with your exact code. I just don’t vote to close as "not reproduced" because I can’t anymore. If your executable "keeps waiting for the two parameters", it is almost certain that it is he who is doing something wrong. Maybe you should edit (or open a new question) focusing on the code of this executable, because your Python code is right.

  • 1

    Problem is exe and not user code

  • Then please do the following test: Fortran Program: program test implicit None Character(LEN=10):: gribfile, gribfile2 read(5,'(a)') gribfile read(5,'(a)') gribfile2 print*, "FILE", gribfile print*, "ARQUIVO2", gribfile2 end program test Program in Python: #! /usr/bin/python import subprocess subprocess.check_call(["test.exe","TQ0126L028","TQ0213L042",shell=True]) ####test.exe (executable from the Fortran program) ####TQ0126L028 (Parametro 1) #########TQ0213L042 (Parametro 2)

Show 8 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.