The other day, I came across an interesting project called sh, which I believe refers to the shell (or terminal). It used to be the pbs project, but they renamed it for reasons I haven’t figured out. Regardless, the sh package is a wrapper around subprocess that allows the developer to call executables a little more simply. Basically it will map your system programs to Python functions. Note that sh only supports linux and mac, whereas pbs supported Windows too.

Let’s look at a couple of examples.

>>> from sh import ls
>>> ls
<command '/bin/ls'/>
>>> ls('/home')
user_one  user_two


I was recently looking into ways to get my screen resolution with Python to help diagnose an issue with an application that wasn’t behaving correctly. In this article, we’ll look at some of the ways to get your screen resolution. Not all of the solutions will be cross-platform, but I’ll be sure to mention that fact when I discuss those methods. Let’s get started!

Using the Linux Command Line

There are several ways to get your screen resolution in Linux. If you do a Google search, you’ll see people using various Python GUI toolkits. I wanted to find a way to get the screen resolution without installing a third party module. I eventually found the following command:

xrandr | grep '*'

I then had to take that information and translate it into Python. Here’s what I came up with:

import subprocess
cmd = ['xrandr']
cmd2 = ['grep', '*']
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
p2 = subprocess.Popen(cmd2, stdin=p.stdout, stdout=subprocess.PIPE)
resolution_string, junk = p2.communicate()
resolution = resolution_string.split()[0]
width, height = resolution.split('x')

Whenever you need to pipe data with Python, you will need to create two different subprocess instances. That is what I did above. I piped the output from xrandr to my second subprocess via its stdin. Then I closed the stdout of the first process to basically flush whatever it had returned to the second process. The rest of the code just parses out the width and height of the monitor.


If you do much system administration, then you know that sometimes you have to write scripts that can move files between servers. I’m not really a system administrator by trade, but I did have to do this sort of thing in some of my programs anyway. Python has several 3rd party packages that provide this ability. We’ll be looking at how to do it with paramiko which depends on PyCrypto (or download PyCrypto from PyPI). (more…)

The bbfreeze package also allows us to create binaries, but only on Linux and Windows. It’s just an easy_install away, so if you plan on following along with the examples in the article, you should go get it. The bbfreeze package includes egg support, so it can include egg dependencies in your binary, unlike py2exe. You can also freeze multiple scripts at once, include the Python interpreter and more. According to bbfreeze’s PyPI entry, it’s only been tested with Python 2.4-2.5, so keep that in mind. However, I was able to use it with Python 2.6 with no obvious problems. (more…)

Soon after getting hired at my current job, my boss sent me a script (which I think was based on this article) about Python and a certain text-to-speech module called pyTTS. This was right after Python 2.5 had released. Anyway, it’s basically a nice wrapper over the win32com module which can communicate with the Microsoft Speech API (SAPI). (more…)