The file is already closed (when the previous with block finishes), so you cannot do anything more to the file. To reopen the file, create another with statement and use the read attribute to read the file.

with open('test_output.txt', 'r') as f2:
    data = f2.read()
    print(data)
Answer from Taku on Stack Overflow
🌐
Python
docs.python.org › 3 › library › io.html
io — Core tools for working with streams
It deals with streams whose bytes represent text, and handles encoding and decoding to and from strings. TextIOWrapper, which extends TextIOBase, is a buffered text interface to a buffered raw stream (BufferedIOBase).
🌐
Python Morsels
pythonmorsels.com › TextIOWrapper
TextIOWrapper‽ converting files to strings in Python - Python Morsels
February 5, 2024 - Ever encountered an _io.TextIOWrapper object when you wished you had a string? That's Python's version of a "text file" object!
Discussions

TypeError: '_io.TextIOWrapper'
Opening a file creates a file object, or more specifically a _io.TextIOWrapper object, it doesn't magically create a dictionary as most files aren't dictionaries. You'll need to parse your file in some way to create a dictionary. I'd suggest saving your file as a JSON file and using the builtin json module. More on reddit.com
🌐 r/learnpython
10
1
November 28, 2022
Implement buffered io streams (io.TextIOWrapper and friends)
Doing I/O (well, at least input) with multibyte characters (like UTF-8) requires buffering support (because if have bytes not forming a complete character, they cannot be passed to the application, buy yet we already have them, and need ... More on github.com
🌐 github.com
4
June 28, 2014
What Is the Role of io.TextIOWrapper in Python Input Handling?
Python developers use io.TextIOWrapper to handle text-based stream operations during input and output activities. More on mindstick.com
🌐 mindstick.com
0
March 29, 2025
Change class from '_io.TextIOWrapper' to 'bytes'
Hello Pythonic minds, I have an text file named “ex23_RawBytes_ConvertedToBytes.txt” These are just text with class ‘_io.TextIOWrapper’ when I checked type in python Any way I can convert them into bytes? The reason I am doing this is that I have all the raw bytes in text but their ... More on discuss.python.org
🌐 discuss.python.org
9
0
February 5, 2024
🌐
Beautiful Soup
tedboy.github.io › python_stdlib › generated › generated › io.TextIOWrapper.html
io.TextIOWrapper — Python Standard Library
io.TextIOWrapper · View page source · class io.TextIOWrapper¶ · Character and line based layer over a BufferedIOBase object, buffer. encoding gives the name of the encoding that the stream will be decoded or encoded with. It defaults to locale.getpreferredencoding.
🌐
Reddit
reddit.com › r/learnpython › typeerror: '_io.textiowrapper'
r/learnpython on Reddit: TypeError: '_io.TextIOWrapper'
November 28, 2022 -

I'm supposed to create a dictionary from the values in a file and search for 'Polly' and remove it if it's in the file,

I kept getting errors so I'm just trying to get it to print the value just so I can see what I'm doing wrong and even at the most simple level I keep getting errors,

I'm in my first semester so this is all still new to me

employees = {}
employees = open('dictionary_values.txt', 'r')
print(employees['Polly'])

Traceback (most recent call last):
  File "main.py", line 3, in <module>
    print(employees['Polly'])
TypeError: '_io.TextIOWrapper' object is not subscriptable

🌐
GitHub
github.com › micropython › micropython › issues › 726
Implement buffered io streams (io.TextIOWrapper and friends) · Issue #726 · micropython/micropython
June 28, 2014 - With this blocking need for input buffering to implement unicode text streams, Python "io" module actually defines buffered streams for binary data either, and buffered streams are used by default.
Author   micropython
🌐
MindStick
mindstick.com › forum › 161312 › what-is-the-role-of-io-textiowrapper-in-python-input-handling
What Is the Role of io.TextIOWrapper in Python Input Handling? – MindStick
March 29, 2025 - The wrapper supports various data ... TextIOWrapper streamlines text interaction within Python by managing data encoding and file stream buffering capabilities to support efficient file and I/O operations....
Find elsewhere
🌐
Python.org
discuss.python.org › python help
Change class from '_io.TextIOWrapper' to 'bytes' - Python Help - Discussions on Python.org
February 5, 2024 - Hello Pythonic minds, I have an text file named “ex23_RawBytes_ConvertedToBytes.txt” These are just text with class ‘_io.TextIOWrapper’ when I checked type in python Any way I can convert them into bytes? The reason …
🌐
Python Morsels
pythonmorsels.com › how-read-text-file
How to read from a text file - Python Morsels
October 4, 2021 - Python has a built-in open function that accepts a filename (in this case we're using this diary980.md file), and it gives us back a file object: >>> f = open("diary980.md") >>> f <_io.TextIOWrapper name='diary980.md' mode='r' encoding='UTF-8'>
🌐
Narkive
comp.lang.python.narkive.com › tHIj43fd › io-textiowrapper-readlines
io.TextIOWrapper.readlines
open('/dev/null','r') <_io.TextIOWrapper name='/dev/null' mode='r' encoding='UTF-8'>
🌐
Python.org
discuss.python.org › ideas
Make io.TextIOWrapper/open support custom line terminators/record separators - Ideas - Discussions on Python.org
December 20, 2023 - My team routinely deals with all sorts of proprietary file formats from various customers. Many of those file formats are records with peculiar record separators that we have to use either str.split, re.split or re.finditer to parse. Wouldn’t it be nice if io.TextIOWrapper, and by extension, the open function, supports alternative characters as line terminator, so we can take advantage of one of the most beautiful idioms of Python, reading “lines” with a for loop over a file-like object?
🌐
University of Toronto
cs.toronto.edu › ~guerzhoy › c4m_website_archive › workshops › W5 › Files and While Loops.html
Files and While Loops
This opens the file named story.txt from the current directory. It is open for reading (that's the r mode) and the type of object is io.TextIOWrapper. Don't stress about the type at all. Just think of it as an open file. The important conceptual idea here is that this object not only knows the contents of the file, but it knows our current position in the file.
🌐
OverIQ
overiq.com › python-101 › file-handling-in-python
File Handling in Python - Python Tutorial - OverIQ.com
The file object returned by open() function is an object of type _io.TextIOWrapper. The class _io.TextIOWrapper provides methods and attributes which helps us to read or write data to and from the file.
🌐
Readthedocs
docspy3zh.readthedocs.io › en › latest › library › io.html
15.2. io — Core tools for working with streams — Python 3 文档(简体中文) 3.2.2 documentation
The TextIOBase ABC, another subclass of IOBase, deals with streams whose bytes represent text, and handles encoding and decoding to and from strings. TextIOWrapper, which extends it, is a buffered text interface to a buffered raw stream (BufferedIOBase).
🌐
Reddit
reddit.com › r/learnpython › [help] why am i getting io.textiowrapper results in this atbs project
r/learnpython on Reddit: [Help] Why am I getting io.TextIOWrapper results in this ATBS project
June 21, 2018 -

I am doing one of the practice projects from chapter 9 of ATBS where you have to write a program that opens all text files in a folder and searches for any line that matches a regular expression.

here is my code

and here is the output

I am nowhere near finished with it yet and I'm sure there are other problems with what I've got so far, but I'm mainly concerned with the io.TextIOWrapper issue in the for loop.

It may be (probably is) caused by the way I'm trying to split the list and read each file, if that's the case, what is a better way to tackle opening each file individually? But I also want to know why the io.TextIOWrapper message is coming up for future reference.

Any help greatly appreciated! Sorry for making you look at hideous code!

🌐
Python.org
discuss.python.org › documentation
Confused about documentation of io.TextIOWrapper encoding - Documentation - Discussions on Python.org
November 15, 2022 - Hi, The io.TextIOWrapper documentation claims that the default encoding is locale.getencoding(). The getencoding function ignores UTF-8 mode. But I thought the whole point of UTF-8 mode was to make UTF-8 encoding the default, especially for text IO. Example: $ LC_ALL=en_US.ISO8859-1 python -X utf8 Python 3.11.0rc2 (main, Sep 13 2022, 00:00:00) [GCC 12.2.1 20220819 (Red Hat 12.2.1-1)] on linux Type "help", "copyright", "credits" or "license" for more information.
🌐
Codecademy
codecademy.com › learn › dacp-python-fundamentals › modules › dscp-python-files › cheatsheet
Python Fundamentals: Python Files Cheatsheet | Codecademy
A Python file object is created when a file is opened with the open() function. You can associate this file object with a variable when you open a file using the with and as keywords. For example: with open('somefile.txt') as file_object: You can then print the content of the file object, file_object with print(). print(file_object) You might see something like this on the output terminal: <_io.TextIOWrapper name='somefile.txt' mode='r' encoding='UTF-8'> To read only one line instead of multiple lines in a Python file, use the method .readline() on a file object that is returned from the open() function.
🌐
Python Forum
python-forum.io › thread-33842.html
Reading data to python: turn into list or dataframe
June 1, 2021 - Hi, I tried 3 different ways to read csv data into python. The 1st and 2nd methods turn data to dataframe and list, and they both work OK The 3rd method, return something with: type(dt):