Search code examples
pythonshellwrapperpython-os

Simple Python shell wrapper script (using os.execv?)


I want to add a wrapper around a specific shell command. This will run in Linux only and I don't care about cross platform support. This code works, but is there a better way to achieve this? Or am I opening myself up to any weird behavior?

import os
import sys

# Do my personal validation here
do_some_validation(sys.argv) 

# Now call the real program
os.execv('/usr/bin/some-command', sys.argv)

Thanks!


Solution

  • You may use subprocess

    import subprocess
    subprocess.call(['/usr/bin/some-command', arg1, arg2])
    

    subprocess is better than os in a way, in that it has more control over execution of a command, whereas os just throws it to bash.