Receiving Output from os.system command

K-Dawg kdawg44 at gmail.com
Mon Jan 19 14:14:35 EST 2009


I am really new to python and am trying to learn it to do some projects.  I
wanted to perform a simple task and am having some trouble with it.  I run
linux in a vm on a windows laptop for work.  I have my laptop screen and an
external monitor.  I move my Ubuntu VM back and forth depending on what I am
working on so I wanted to write a little python script that changed the
resolutions depending on what the current resolution is.

I have:

#! /usr/bin/python

import sys
import os
import re

re_currentResolution = re.compile(r'current \d{4}\sx\s\d{3,4}')
xrandr_output = os.popen('xrandr').readlines()
currentRes = re_currentResolution.search(xrandr_output)
print currentRes.group(0)

I just want to grab the xrandr output as a string and parse it with the
regex.  This will give me a string with the current resolution, for instance
"current 1024 x 768".  I would then split that up and call back to the
os.system('xrandr -s 1280x1024').  If the resolution was "current 1280 x
1024" then I would parse that and call back to os.system('xrandr -s
1024x768').

However, when I try the line (as in the code above):

currentRes = re_currentResolution.search(xrandr_output)

it is complaining that xrandr_output is not a string.  (however, if I do a
print xrandr_output it prints fine)

I instead get the following error:

Traceback (most recent call last):
  File "change_res.py", line 9, in <module>
    currentRes = re_currentResolution.search(xrandr_output)
TypeError: expected string or buffer

What am I doing wrong?

Thanks.

Kevin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090119/e909c665/attachment.html>


More information about the Python-list mailing list