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 OverflowWhat Is the Role of io.TextIOWrapper in Python Input Handling?
python - How to read/print the ( _io.TextIOWrapper) data? - Stack Overflow
TypeError: '_io.TextIOWrapper'
python - How to convert _io.TextIOWrapper to string? - Stack Overflow
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
You need to use the output of f.read().
string = f.read()
I think your confusion is that f will be turned into a string just by calling its method .read(), but that's not the case. I don't think it's even possible for builtins to do that.
For reference, _io.TextIOWrapper is the class of an open text file. See the documentation for io.TextIOWrapper.
By the way, best practice is to use a with-statement for opening files:
with open("document.txt", "r", encoding='utf-8-sig') as f:
string = f.read()
This is good:
with open(file, 'r', encoding='utf-8-sig') as f:
data = f.read()
This is not good:
with open(file, 'r', encoding='utf-8-sig') as file:
data = file.read()
That expression for line in file splits (streams more accurately) the file by the new line delimiter until it reaches EOF. Think of it a stream that reads until it's a new line, and then returns the characters it just read.
File-like objects are iterators that produce a line of text on each iteration. Iterators in general just means "things you can loop over exactly once"; files differ from this pattern on insofar as they can (depending on what the represent) be seeked, which would reset the iterator to a new position in the file.
To be clear, they are not sequences; the term "sequence" has specific meaning, and includes the ability to index it, iterate it multiple times in a row or in parallel, all without manually fixing up state.
I am writing function to help me printing data in text files with different types. in my code for function def write(self, file:io.TextIOWrapper): i want to specify type of my file variable to improve my documentation and code readability. Any idea how to do this?
class Node:
Id: int
X: float
Y: float
Z: float
def __init__(self, Id: int, x: float, y: float, z: float):
self.Id = int(Id)
self.X = float(x)
self.Y = float(y)
self.Z = float(z)
def write(self, file:io.TextIOWrapper):
file.write(f"{self.Id},{self.X:0.4f},{self.Y:0.4f},{self.Z:0.4f};\n")
def __str__(self):
return f"{self.Id}:{self.X},{self.Y},{self.Z}"