Python打开(文件)的类型是什么 [英] Python What's type for open(file)
本文介绍了Python打开(文件)的类型是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用isinstance
内置函数来判断open(file)
的类型.
怎么做?
谢谢!:D
解决方案
在 Python 2.x 中,所有文件对象的类型都是 file
:
然而,在 Python 3.x 中,普通文件对象的类型为 <代码>io.TextIOWrapper:
<预><代码>>>>类型(打开('文件.txt'))<类'_io.TextIOWrapper'>>>>>>>从 io 导入 TextIOWrapper>>>isinstance(open('file.txt'), TextIOWrapper)真的>>>I want to use isinstance
built-in function to judge the type of open(file)
.
How to do that?
Thanks! :D
解决方案
In Python 2.x, all file objects are of type file
:
>>> type(open('file.txt'))
<type 'file'>
>>>
>>> isinstance(open('file.txt'), file)
True
>>>
In Python 3.x however, normal file objects are of type io.TextIOWrapper
:
>>> type(open('file.txt'))
<class '_io.TextIOWrapper'>
>>>
>>> from io import TextIOWrapper
>>> isinstance(open('file.txt'), TextIOWrapper)
True
>>>
这篇关于Python打开(文件)的类型是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文