actually reading and displaying a text file

hi im back again in my quest to learn python for my flight sim

i know its possible to actually read say a txt file but is it possible to display the contents and how thanks t#for your help

Just assign the result of {file}.read or {file}.readline to a variable :

fin=open('c:/test.txt')
line=fin.readline()
print "line = ",line

#
#
#or  just print directly :

print  fin.readline()


Mike

when i do that i just get some green text saying “in built function” do i need to compile my program?

Where do you want to display the text? Is this for the game engine or when running Blender?

What are you using to run that script?

I tested it with both the Windows python “command line” (python.exe) and from within Blender, both work (and I’m surprised it works in Blender without any “import”) statement.

Are you sure you typed in the parenthesis at the end of “readline” ? :


line = fin.readline<b>()</b>   

It sounds like you didn’t. If you run :
(note no () on fin.readline


line = fin.readline
print line

You’ll get output something like :

line = <built-in method readline of file object at 0x015C0C80>

Mike

im sure i did il give it another try see what happens

it just comes up with one line now not all of the file what i want printed ?

and i tried both versions of it sorry about this

fi=open('c:/temp/test.txt')

for line in fi.readlines(): print line

have a good day.

Just assign the result of {file}.read or {file}.readline to a variable :

Useful, but you don’t need to do it.
print file.read() will work too.
In fact:
print open(‘c:/test.txt’).read()

Will work too. Best to save it to a variable, yes, but not necessary.

Also, very importantly, close the file. filename.close() does that.