Execute terminal commands in python3

BrandonMayU picture BrandonMayU · Jan 1, 2016 · Viewed 13.8k times · Source

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?

Answer

vesche picture vesche · Jan 1, 2016

Use the subprocess module:

import subprocess
subprocess.Popen(["fswebcam", "image.jpg"])