Execute terminal commands in python3


I am on a Raspberry Pi, and I am using a program called fswebcam, which allows you to take pictures with a webcam.

~$ fswebcam image.jpg

That command if entered in terminal takes a picture and saves it to your computer, however I want to build a simple python program that can access the terminal and execute that same command as I have listed above.

I have tried to import os and use os.system('fswebcam image.jpg') But it isn’t working for me.

How can I have python execute terminal commands?

Asked By: BrandonMayU



Use the subprocess module:

import subprocess
subprocess.Popen(["fswebcam", "image.jpg"])
Answered By: vesche