I'm working on a python script that will start and stop some services based on whether or not a display is plugged in. For this I want to assign the output of "tvservice -n" to a variable, and use this information to determine further actions. I'm using a raspberry pi zero w and I have run my script on both python 2.7.13 and 3.5.3.
I have tried many different approaches from my research on this site, this is one example Assign output of os.system to a variable and prevent it from being displayed on the screen
When I run the script it first runs the command, and then shows the variable output (which is null). The code below is one of the simplest tests I did, I also tried subprocess.run, subprocess.popen, subprocess.check_output etc etc...
import os
var = os.popen('tvservice -n').read()
print "result: ",var
Actual results from running my code:
[E] No device present
result: