Slated for inclusion in Python 2.5, ctypes offers a foreign function interface (FFI) for bring shared libraries in, and wrapping them up so that you can access them through Python. It’s actually an amazing piece of work. It supports the shared libraries on every major platform, and makes it a snap to gain access to a few functions that you may need. I recently found that we’ll need the ctypes module for a project that I’m working on (I need it for PyVISA), so I also used it solve another problem: getting the amount of free disk space under a certain path. On nix type systems, it’s easy: use os.statvfs(). On Windows, you really need access to GetDiskFreeSpaceEx(). Well, the team didn’t want to include pywin32 just to gain access to this *one function, so we used ctypes instead. That lead us to the following code snippet which implements exactly what we need on both Linux and Windows:
The above code provides the required information, and works on both platforms. It’s easy, simple, and we didn’t have to include another module to make use of it. There is one drawback though. Going through this sort of interface can be very slow. However, we only ask for the disk space occassionally, so speed was not an issue for us.