Blog / Python

How to zip an entire directory with Python

January 22, 2011

I’m finally done with the Python project I’m working on and now just working on the build script. I want my build script to zip the entire build directory at the end so at first I got lazy and I figured I’d just call an external zip application (7-Zip) and let it do all the work. This would of course require the person doing the build to have 7-Zip installed, so I decided …

How to use configuration files in Python

December 31, 2010

I’ve been doing quite a bit of Python lately and the application I’m working on right now is actually a rewrite of scripts that we wrote over a year ago. The scripts started out to be pretty simple but they have now grown to a point that to be able to maintain and extend them quickly they’ll need to be completely rewritten with a new design.

One of the things we’re adding is a configuration …

How to check if a file is locked in Python

December 14, 2010

My colleague showed me a trick on how to do this earlier. What I was trying to do is poll a folder for a bunch of files and if they’re not there keep polling until they arrive, then process them. The problem is before they can be processed we need to be sure that the file transfer is complete.

The trick on how to do this is to try and open the file once it …

Python function for displaying a list of dictionaries in table format

December 3, 2010

I just spent like 2 hours writing this function, but it was worth it!

This cut down a lot of duplicate code on the project I'm working on. I figured I'd post it here in case someone is trying to do the same thing. I'm sure my code is not optimal so if you have any suggestions on how to improve it please feel free to comment :).

from operator import itemgetter

def format_as_table(data,
                    keys, …

How to retrieve the name of the calling module in Python

November 23, 2010

I was writing some Python code yesterday and I was wondering if there’s a way to retrieve the name of the module making the call to another module so I can log it automatically. I found this post that gives an example on how to do it using the built-in inspect module (I had to modify the sample code a little bit to make it work).

It looks like you can use this module to …