run python in shell script code example
Example 1: executing shell commands from python script
import subprocess
subprocess.run(["bash", "testShell.sh"])
list_files = subprocess.run(["ls", "-l"])
print("The exit code was: %d" % list_files.returncode)
Example 2: how to execute bash commands in python script
import subprocess
subprocess.call(["sudo", "apt", "update"])
Example 3: call shell script from python
import subprocess
subprocess.call(["./shell.sh"])
Example 4: how to run python in terminal
$ python3 helloworld.py
$ python helloworld.py
Example 5: python run shell command
import subprocess
process = subprocess.Popen(['echo', 'More output'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
stdout, stderr
Example 6: to run python script in terminal i want to type python insted of python3
$ python --version
Python 2.7.6
$ python3 --version
Python 3.4.3
$ alias python=python3
$ python --version
Python 3.4.3