coconutm1lk
Posts: 3
Joined: Tue Mar 19, 2019 8:41 pm

How to use command terminal code in Python program?

Tue Mar 19, 2019 8:52 pm

Recently I downloaded the LIRC library (following the instructions here: http://alexba.in/blog/2013/01/06/settin ... spberrypi/) and while it works properly, I need to use it for a non-remote application. I am trying to read input from multiple IR receivers. I have verified that I can use this library to read the input from multiple IR receivers, but I need to write Python code now to read the continuous input and also identify which specific receiver/s are getting input. Normally, the way to start the IR reading is to go to the command terminal and type

sudo /etc/init.d/lirc stop
mode2 -d /dev/lirc0

But I need to be able to initiate this inside a Python program, and also perhaps tweak the file which is reading input so that I can differentiate which GPIO pins specifically are getting input. As I am only a beginner, however, I am not sure how I would go about being able to do in Python what is usually achieved through the command prompt. Furthermore, I am not sure how I could read from individual GPIO pins using this library as there's not a ton of helpful guides on this library online past remote applications. Any help would be deeply appreciated, thank you in advance.

Andyroo

Re: How to use command terminal code in Python program?

Tue Mar 19, 2019 9:33 pm

To call command line programs from Python you need to import the os module https://www.pythonforbeginners.com/os/pythons-os-module
To make the actual call, use the subprocess method https://www.pythonforbeginners.com/os/s ... nistrators

robbes
Posts: 144
Joined: Sun Jan 20, 2013 7:11 pm
Location: Canada - off the west coast

Re: How to use command terminal code in Python program?

Wed Mar 20, 2019 4:41 pm

The plumbum library might be convenient for your use case:
https://plumbum.readthedocs.io/en/latest/

coconutm1lk
Posts: 3
Joined: Tue Mar 19, 2019 8:41 pm

Re: How to use command terminal code in Python program?

Wed Mar 27, 2019 4:46 pm

Thank you both so, so much! I ended up going with Andyroo's solution as it does not require external libraries, but I appreciate the input from both of you. I also got a python program working which can do shell commands now, which is awesome! I apologize for my long questions because I didn't know that "shell commands" was the right term I was looking for, I'm still a huge noob. Anyways thanks a ton :D

Return to “Python”