

In this tutorial we are going to see how to execute Linux shell commands from your python script. This may be helpful as you can program the required modules in shell. This would be helpful to automate the tasks in Raspberry Pi.
Executing shell commands
The Linux shell commands can be easily executed using the function os.system(). Here In the example I am executing the date linux command from python. It prints the date as output.
Example code
shell.py
import os def main(): os.system("date") if __name__=="__main__": main()
Executing shell scripts
Shell scripts can also be easily executed using the same os.system() function. Here in the example code I am executing a linux shell script which prints “Hello Python from Shell” .
Example code
hello.sh
#Linux shell Script echo "Hello Python from Shell";
shell.py
import os def main(): os.system("sh hello.sh") if __name__=="__main__": main()
Steps to execute the code
1. Open the Raspberry Pi terminal.
2. Then type nano program.py
3. Now type the python program and save it.
4. Execute using the command python program.py
Any questions comment here 🙂