Getting value for a variable

I am trying to get value for a variable that has more than 1 layer when I look at it in Pycharm. The variable looks like this

host_file = {TextIOWrapper} <_io.TextIOWrapper name=‘C:\TEMP/HOSTLIST.TXT’ mode=‘r’ encoding=‘cp1252’>
buffer = {BufferedReader} <_io.BufferedReader name=‘C:\TEMP/HOSTLIST.TXT’>
closed = {bool} True
encoding = {str} ‘cp1252’
errors = {str} ‘strict’

The variable is host_file, the next layer is buffer and below that is closed, encoding and errors. I want to check the value of closed. I tried host_file.buffer.closed, but it errors.

Any ideas?

What does this mean? That is: exactly how do you “look at it in Pycharm” - and what code do you use in order to get the variable to this state?

I mean when I’m debugging Python program in Pycharm.

The host_list is listed as a Value, then indented is the Value below host_list and below that indented are Value of a primitive type which is Closed. Normally it looks like a variable in the program lists as a Value of a primitive type, but it seems like some variables have layers that have 3 yellow bars on the left of the variable and the primitive types have a 01 on the left of a variable.

I guess you would only know if you were in Pycharm debug window.

closed is an attribute of host_file, not of buffer.

>>> f = open('expiry.txt', 'r', encoding='utf-8')
>>> dir(f)
['_CHUNK_SIZE', '__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_checkClosed', '_checkReadable', '_checkSeekable', '_checkWritable', '_finalizing', 'buffer', 'close', 'closed', 'detach', 'encoding', 'errors', 'fileno', 'flush', 'isatty', 'line_buffering', 'mode', 'name', 'newlines', 'read', 'readable', 'readline', 'readlines', 'reconfigure', 'seek', 'seekable', 'tell', 'truncate', 'writable', 'write', 'write_through', 'writelines']

>>> type(f.closed)
<class 'bool'>
>>> f.closed
False
>>> f.close()
>>> f.closed
True

I don’t use Pycharm, but it looks like hierarchies of objects are displayed at increasing depth, so anything on the same indentation should belong to the same parent.

1 Like

See io — Core tools for working with streams — Python 3.12.3 documentation that tells you that you are checking the wrong object, as I understand it.

I am trying to get value for a variable that has more than 1 layer when
I look at it in Pycharm. The variable looks like this

 host_file = {TextIOWrapper} <_io.TextIOWrapper name='C:\TEMP/HOSTLIST.TXT' mode='r' encoding='cp1252'>

The variable is host_file, the next layer is buffer and below that is
closed, encoding and errors. I want to check the value of closed. I
tried host_file.buffer.closed, but it errors.

If host_file is a TextIOWrapper, then that inherits from
TextIOBase which inherits from IOBase, which has a .closed
attribute. So try:

 host_file.closed

Docs:

TextIOWrapper: io — Core tools for working with streams — Python 3.12.3 documentation

and follow the references from there to TextIOBase and then to
IOBase. You’re basicly going from TextIOPWrapper, which is a
concrete class wrapping an open file to the generic “do I/O with text”
to “do I/O”. The .closed attribute is generic to all files and
therefore you have to get to IOBase to find its docs.

That did the trick. I thought I tried that on another variable, but it didn’t work.

Thank you and anyone else who tried to help people that do not have a lot of Python knowledge, but need to get a solution to their problem. It really helps advance the language use and the people using it.

~WRD0000.jpg