Ho do I write a script to remove loose geometry from all objects? I'm trying this:

import bpy

bpy.ops.object.select_all(action='DESELECT')
for o in bpy.data.objects:
    o.select = True
    bpy.ops.object.mode_set(mode='EDIT')
    bpy.ops.mesh.select_all(action='DESELECT')
    bpy.ops.mesh.select_mode(type='VERT')
    bpy.ops.mesh.select_loose()
    bpy.ops.mesh.delete(type='VERT')
    bpy.ops.object.mode_set(mode='OBJECT')
    o.select = False


when I manually assign an object like o = bpy.data.objects[3] and then do every step manually in the python console it works. But with the loop it doesn't work. Is it a bug or am I missing something?
Set the active object:

bpy.context.scene.objects.active = o


and you should check wether the object is a mesh object:

if o.type == 'MESH': 
    ...