reading text editor file how to ?



import bpy

#    to find special characters

la1=len(bpy.context.screen.areas)
print ('Lenght areas =', la1)
print ()

for j in range(0,la1):
    #a1=bpy.context.screen.areas[j].active_space.text 
    a1=bpy.context.screen.areas[j].type
    print (' j=',j,' area=',a1)


k=0
    
for j in range(0,la1):
    
    print('j=',j ,'  area type=',bpy.context.screen.areas[j].type)
    if bpy.context.screen.areas[j].type=="TEXT_EDITOR":
        k=j
        print('This is area number k =',k)
        

text = bpy.context.screen.areas[k].active_space.text

import bpy
for i, l in enumerate(text.lines):
    line = l.body
    print(i, line)



how do you identify the different files in the text editor ?
and which one is the active one to read data?

thanks

Forget about screen areas, just read bpy.data.texts directly.


import bpy

my_text = bpy.data.texts.get("my_text_file.txt")
for line in my_text.lines:
    print(line.body)

if you have more then one text file in editor
I thought you had to select it first somehow !
but may be it has been change !

how do you get the right text file in text editor ?

and how do you copy file name in editor bottom header?

and how do you list all text file name ?

thanks

This is unnecessary:

for line in my_text.lines:
    print(line.body)

You can get the full text with

my_text.as_string()

if you have more then one text file in editor
I thought you had to select it first somehow

You should get clear about what you actually mean, get the text of a text datablock or get the text of one of the text datablocks that is currently show in a text editor.

how do you list all text file name ?

Text datablock access path: bpy.data.texts
Reference to a text datablock of a text editor (“active text block”): SpaceTextEditor.text

and how do you copy file name in editor bottom header?

I don’t think you mean file name but text datablock name: Text.name
The file path can be accessed via Text.filepath,
if you want the file name only, you can e.g. use bpy.path.basename(Text.filepath)
Since strings are immutable, it’s always a copy.

not certain what words to use here !

when you use this command
my_text = bpy.data.texts.get(“my_text_file.txt”)
you must give the file name in text editor

but suppose that you have more then one text files in editor
how can you get data text from one file?

is it necessary to select the file first then you can read it or not ?
or just giving the name is enough then you can read it !

I found that you must use Ctrl-C on the file name in text editor there is no copy on the field name on win !

thanks