Issues switching modes with Blender's Python BPY command

Hi, I need to make a change to many blender files so I wrote a script using the bpy library to simplify the process. However, the command that changes the mode to edit mode does not work and I have to manually make the switch. This same command works on other simpler empty blender files so I was thinking maybe there’s something off with my blender file itself?? I’ll attach it to this post.
Here is the beginning bit of the script (up until the part where I manually have to change the mode):

bpy.ops.object.mode_set(mode=‘OBJECT’)
bpy.ops.object.empty_add(type=‘SPHERE’)
TARGET = bpy.context.active_object
TARGET.name = ‘TARGET’
TARGET.location = (0, 0.8, 1.3)
TARGET.scale = (0.2, 0.2, 0.2)
for obj in bpy.data.objects:
obj.select = False

armature = bpy.data.objects[31]
context = bpy.context
scene = context.scene
scene.objects.active = armature
bpy.ops.object.mode_set(mode=‘EDIT’)

Would really appreciate some help with this issue that I’m having!!

blender version?

Use this to check whether you are in edit (this works on the currently active, but can be used on any object from data)

bpy.context.object.mode == "EDIT":

if in not in edit you can try this

bpy.ops.object.editmode_toggle()

btw make sure you typed it properly, you might have typo there

bpy.ops.object.mode_set(mode='EDIT')

Thanks for your reply.
Yeah that was the first thing I tried… it even gives me the message {‘Finished’} on the Python Console but stays in Object mode.
If I create a new/blank Blender file, these commands work just fine and switch to Edit mode, but doesn’t on all my armature files.
On my armature blender files, I can switch from Object to Pose mode and vise versa with the basic commands but if I try to switch to Edit mode from either one, it gives the Finished message and goes into Object mode instead.
I believe its some kind of bug within my blender file (that’s why I included it on the post) because I already tried all the commands that should change the mode.