How to find what text file is open in text editor
Active text datablocks are referenced by TextEditor spaces:

bpy.types.SpaceTextEditor.text

for area in bpy.context.screen.areas:
    if area.type == 'TEXT_EDITOR':
        space = area.spaces.active
        print(repr(space.text))

# Example output with one visible text editor:
# bpy.data.texts['ui_menu_simple.py']