Enno
Enno

Reputation: 21

Python Error Read Files <built-in method read of _io.TextIOWrapper object at 0x0000024B969DA4D0> Python 3.10

I´m trying to read a .txt file in Python. I have this code:

file = open("LESEN.txt","r")
print(file)

When I try to run it, this error comes up:

<built-in method read of _io.TextIOWrapper object at 0x0000024B969DA4D0>

I can remember that it works in a older Python version. What am I doing wrong?

Upvotes: 1

Views: 6693

Answers (2)

Chaitanya Sampathi
Chaitanya Sampathi

Reputation: 1

f = open("INTERVIEW_SCHEDULES.txt","r") # You can remove r on the open(), it is read mode by default.
content = f.read()
print(content)
f.close()

Upvotes: 0

BrokenBenchmark
BrokenBenchmark

Reputation: 19242

There are two issues with your code:

  1. You're printing out the memory address of the file handle, not the contents of the file. You want file.read() for that.
  2. You aren't closing the file anywhere -- it's good to use context managers for this, which will automatically handle the closing for you.

Here is a code snippet that resolves both issues:

with open("LESEN.txt", "r") as file:
    print(file.read())

Upvotes: 3

Related Questions