Category Archives: python

Installing PIL in virtualenv in Ubuntu

To install PIL in virtualenv in ubuntu, these are the steps:

  1. install the dependencies needed to build PIL

    sudo apt-get build-dep python-imaging
  2. then run these commands

    sudo ln -s /usr/lib/i386-linux-gnu/ /usr/lib/
    sudo ln -s /usr/lib/i386-linux-gnu/ /usr/lib/
    sudo ln -s /usr/lib/i386-linux-gnu/ /usr/lib/

    If you’re using 64bit system, switch i386-linux-gnu to x86_64-linux-gnu.

    If you don’t do it, you might see these messages when after building PIL, and you won’t have jpeg support.

    *** JPEG support not available
    *** ZLIB (PNG/ZIP) support not available
    *** FREETYPE2 support not available
  3. Last command, install PIL

    pip install PIL

Replace Space With Underscore in Filename

This is merely my personal note. I was about to rename all my files under a directory. I need to replace the space with underscore character ('_'). My first thought was a simple bash script to do that. Apparently, it’s been very long time since my last bash coding session. I spent 15 minutes reading how to read all files and rename them. And I got nothing.

Luckily, I know python. Stupid me. Why didn’t I think it at first time. It was couple minutes of python and all the spaces were replaced by underscores. Thanks to python. All I did were

$ python
Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49) 
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> files = os.listdir('./')
>>> for f in files:
...     os.rename(f, f.replace(' ', '_'))

Or if you want to save in a script, you could you this


import os
import sys

files = os.listdir(sys.argv[1])
for f in files:
    os.rename(f, f.replace(' ', '_'))

The script takes the directory path as the argument. You could modify the script to use regex to have a better rename rule 🙂

Tagged , ,

Python by Example

Python by example.

Definitely great resource for me who just started to learn python.