-1

I have been working on a project to run terminal command with a python file, for example, I have a file called getWeight.py, it requires 2 parameters when executing it:

Here is what I run inside the terminal:

python3 getWeight.py param1 param2

I also have a file named automation.py, which will execute the command above, but it is inside a conditional:

while loop_while != 0:
    if loop_while == 1:
        //getWeight.py param1 param2
    if loop_while == 2:
        //getLed.py param1

    
    print("\nSelect your next option:")
    print("(1) - getWeight")
    print("(2) - getLed")
    print("(0) - Exit")

    loop_while = int(input('Option: '))
Xiddoc
  • 3,369
  • 3
  • 11
  • 37
  • 2
    It's not clear what the title and body of this question have to do with each other. In the title you talk about executing terminal commands, but nothing in the body has any obvious relationship to terminal commands. Do remember to focus on what your specific problem is, not just what the code you were writing when you encountered that problem was. – Charles Duffy May 11 '21 at 23:02
  • 2
    Does this answer your question? [Running windows shell commands with python](https://stackoverflow.com/questions/14894993/running-windows-shell-commands-with-python) – dev55555 May 11 '21 at 23:03
  • 1
    You should `import` your scripts rather than running them as terminal commands – OneCricketeer May 11 '21 at 23:14
  • look into `argparse` and `subprocess` python libs – Bruno May 11 '21 at 23:14

1 Answers1

0

Maybe you can try the "os" library. I use it to perform some basic commands. I believe it might prove helpful for your needs.

For example we type "cls" in a Windows system to clear to command window. In Python you can simply use this snippet:

import os
os.system("cls")

As it is obvious, maybe you can try expressing the terminal command as a string variable. Then pass the string to the "os.system()" method. I haven't tried it myself, but I believe that it may work. Good luck with that!

kucar
  • 245
  • 3
  • 13