python3.9 with open as ‘r‘ 模式下内容出现 _io.TextIOWrapper 解决方案

python3.9 with open as 'r' 模式下内容出现 _io.TextIOWrapper 解决方案

在使用类似以下代码时

with open ( 'textsave.txt','r', encoding='UTF-8' ) as object_list_secondn:

        object_list_secondn.read()

        object_list_secondn=str(object_list_secondn)

        print(object_list_secondn)

with open ( 'textsave.txt','r', encoding='UTF-8' ) as object_list_secondn:
        object_list_secondn.read()
        object_list_secondn=str(object_list_secondn)
        print(object_list_secondn)

打印结果为

<_io.TextIOWrapper name='textsave.txt' mode='r' encoding='UTF-8'>

<_io.TextIOWrapper name='textsave.txt' mode='r' encoding='UTF-8'>

没有出现预想的打印结果

是因为object_list_secondn=文件信息

只需让object_list_secondn.read()  ---> object_list_secondn=object_list_second.read()

变成

with open ( 'textsave.txt','r', encoding='UTF-8' ) as object_list_second:

        object_list_secondn=object_list_second.read()

        object_list_secondn=str(object_list_second)

        print(object_list_second)

with open ( 'textsave.txt','r', encoding='UTF-8' ) as object_list_second:

        object_list_secondn=object_list_second.read()

        object_list_secondn=str(object_list_second)

        print(object_list_second)

你可能感兴趣的:(开发语言,python)