Setting objects into edit mode often seems to fail in python script I am only just starting to learn/use python in blender.  I have the following script 

import bpy

for obj in bpy.data.objects:

    if obj.name[0:4] == "Text" and obj.type == "MESH":

        bpy.context.scene.objects.active = obj
        bpy.ops.object.mode_set(mode = 'EDIT')
        print(bpy.context.mode)


        #bpy.ops.mesh.select_all(action = 'SELECT')
        #print(bpy.ops.mesh.remove_doubles(0.0001))
        bpy.ops.object.mode_set(mode = 'OBJECT')


I am trying to work out why I keep getting a context error for bpy.ops.mesh.select_all so I added the print(bpy.context.mode) line and commented out the line that was failing (plus the next line which takes action)

what I get in the console (this script is being run from the console rather than run in place) is a mixture of 'OBJECT' and 'EDIT_MESH' outputs.. suggesting that mode_set is failing a lot of the time.

What am I doing wrong?

I am using blender 2.68a on Windows 7.

Edit I discovered the actual cause of my issue (hinted at by Adhi's edit) and wanted to add it to my post so that others can take something away from this - the objects that were not being switched into edit mode were on another layer.  Once I made all layers active my script was able to switch all objects into edit mode.  So basically an object in an unselected layer cannot be set into edit mode.

I am also grateful for the other help and suggestions, they have been useful to help me learn python scripting within blender which is something I would love to use more extensively.
As mentioned by dimus, your problem is solved by adding keyword to the mesh.remove_doubles operator's first argument, meant as threshold property value. With that little bug fixed, your script works properly.

What causes the error is that, keyword arguments are used to set operator properties, and positional arguments to set how the operator is called (further explained in the API docs). By not using a keyword for the first argument, you're turning it into a positional argument, thus interpreted as operator execution context which is an error.

EDIT: Another possible error is iterating over bpy.data.objects, which comprises of all objects in every scene in a file. The script is bound to fail if there's more than one scene in a file containing a MESH object, because it'll try to make object from a different scene the active object of current scene which is contextually incorrect. You should iterate over bpy.context.scene.objects instead.

Here's the amended code, can't see any other bug:

import bpy

scene = bpy.context.scene
scene.layers = [True] * 20 # Show all layers

for obj in scene.objects:
    if obj.type == 'MESH':
        scene.objects.active = obj

        bpy.ops.object.mode_set(mode='EDIT')
        bpy.ops.mesh.remove_doubles(threshold=0.0001)
        bpy.ops.object.mode_set(mode='OBJECT')