简体   繁体   中英

What type are file objects in Python?

如何使用isinstance确定文件对象的“类型”,例如在表达式中:

>>> open(file)

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

As stated in the documentation for open :

Open a file, returning an object of the file type described in section File Objects .

Thus, open returns a file , and you should use isinstance(foo, file)

Its type is file . You can tell by the output of type(open("file","w"))

This answer builds upon the previous answer by user2555451 and its comments. The short answer is to pick your preference from the IO class hierarchy .

For text files , io.TextIOBase can be acceptable:

>>> import io

>>> type(f := open('/tmp/foo', 'w'))
<class '_io.TextIOWrapper'>
>>> isinstance(f, io.TextIOBase)
True

>>> f.__class__.__bases__
(<class '_io._TextIOBase'>,)
>>> f.__class__.__mro__
(<class '_io.TextIOWrapper'>, <class '_io._TextIOBase'>, <class '_io._IOBase'>, <class 'object'>)

For text files, avoid io.TextIOWrapper because it does not also work for io.StringIO :

>>> isinstance(io.StringIO('foo'), io.TextIOWrapper)
False
>>> isinstance(io.StringIO('foo'), io.TextIOBase)
True

For binary files , io.BufferedIOBase can be acceptable:

>>> import io

>>> type(f := open('/tmp/foo', 'wb'))
<class '_io.BufferedWriter'>
>>> isinstance(f, io.BufferedIOBase)
True

>>> f.__class__.__bases__
(<class '_io._BufferedIOBase'>,)
>>> f.__class__.__mro__
(<class '_io.BufferedWriter'>, <class '_io._BufferedIOBase'>, <class '_io._IOBase'>, <class 'object'>)

For binary files, avoid io.BufferedReader or io.BufferedWriter because they do not also work for io.BytesIO :

>>> isinstance(io.BytesIO(b'foo'), io.BufferedReader)
False
>>> isinstance(io.BytesIO(b'foo'), io.BufferedWriter)
False
>>> isinstance(io.BytesIO(b'foo'), io.BufferedIOBase)
True

To support both text and binary files , io.IOBase is acceptable:

>>> import io

>>> isinstance(open('/tmp/foo', 'w'), io.IOBase)
True
>>> isinstance(open('/tmp/foo', 'wb'), io.IOBase)
True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM