1

I just started learning Python today and couldn't find a good example online to help me understand os.execve(path, args, env) properly.

How do I use this method (os.execve) to achieve the following task in Python 3.4?
Execute an external command (this command is not some windows command like mkdir, cd... It's a custom command), its location is C:\blah and it takes 5 command line arguments.

Any simpler example of using this command would be much appreciated.

5
  • 2
    The os.exec* function will stop your program from executing. Is that really what you want? Commented Sep 15, 2014 at 12:14
  • fine how to exexute the external command without stopping my program ? Commented Sep 15, 2014 at 12:29
  • "I just started learning Python today and couldn't find a good example online to help me understand os.execve(path, args, env) properly.". Python has a superb online documentation, if you can find it, you are doing something wrong... Link to os.execve documentation Commented Sep 15, 2014 at 13:13
  • @KurzedMetal The os.exec* has a horrible documentation. Which type should the args be? What should be the env? If the documentation bothered to give an example, perhaps I wouldn't be banging the wall with an AttributeError: 'str' object has no attribute 'keys' error right now. Commented Dec 25, 2020 at 8:04
  • UPD. Ok, the following is a working example to execute a python script from python: os.execv('./myscript.py', ['any_arbitrary_tag', '--my-argument', '--another-argument 42']) The env means the PATH variable of current environment and can be retrieved by os.environ.get('PATH') Commented Dec 25, 2020 at 8:24

1 Answer 1

3

You want to use subprocess:

import subprocess
subprocess.check_call(["C:\my program.exe", "all", "my", "args"])

os.exec* replaces the current program with another one. It has is uses, but its usually not what you want.

Note that there are several variants here:

  1. call just calls the program.
  2. check_call calls the program and throws an exception if it fails.
  3. check_output calls the program, throws an exception if it fails, and returns the output of the program.

More advanced use cases can be handled by subprocess.Popen objects.

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

8 Comments

how would this work for a command like " login -u john -ac regular -p password" where login is login.exe ,the command
subprocess.call(["login", "-u", "john", "-ac", "regular", "-p", "password"]). (You may need to pass the full path to login)
@DhiwakarRavikumar, yes.
that was stupid to ask I should've tried first. it works on windows as well, is there any simpler way to write this as opposed to the CSV style ? its something that could be improved :)
The best way to pass multiple arguments is to use shlex: subprocess.call(shlex.split("/path/to/cmd --with --many args -p password"))
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.