Python 2.X 的 types.FileType 如何移植到 Python 3.X?

2018-11-19 15:41:24 +08:00
 acone2003
最近下载了一个 parallel python ( PP )模块,是在 python 2 下运行的,现在想移植到 python 3 中,有很多地方需要修改,其中有一个地方不知道怎么改了,望各位不吝给予帮助:
pptransport.py 中有一句:
import types
if isinstance(r, types.FileType) and isinstance(w, types.FileType):
self.r = r
self.w = w
查了一下在 python 3 中已没有了 FileType 属性,该怎么办?
1995 次点击
所在节点    Python
4 条回复
no1xsyzy
2018-11-19 16:04:01 +08:00
https://docs.python.org/2/library/types.html?highlight=types%20filetype#types.FileType
> types.FileType
> The type of open file objects such as sys.stdout; alias of the built-in file.

>>> type(sys.stdout)
<class '_io.TextIOWrapper'>
warcraft1236
2018-11-19 16:05:32 +08:00
查了一下,pip install filetype 还是有的,而且文档中给了用法
chenstack
2018-11-19 17:04:58 +08:00
试了一下,可以用 io.IOBase 判断
from io import IOBase
isinstance(r, IOBase)
acone2003
2018-11-20 09:02:19 +08:00
谢谢 chenstack,OK!同时也谢谢楼上两位!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/509287

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX