user9717300
user9717300

Reputation:

Python .txt file not reading correctly

I have been started to have a look at using .txt files in my code but when I use these lines of code it just brings back this, <_io.TextIOWrapper name='names.txt' mode='r' encoding='cp1252'>

Here is the code I am using:

f = open('names.txt','r')
f.read()
print(f)
f.close()

Upvotes: 0

Views: 36

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195438

Store result of file.read() to variable, and then print the variable.

For example:

f = open('names.txt','r')
data = f.read()
print(data)
f.close()

You can also use with keyword to not close the file explicitly:

with open('names.txt','r') as f:
    print(f.read())

Upvotes: 1

Related Questions