V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
acone2003
V2EX  ›  Python

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

  •  
  •   acone2003 · 2018-11-19 15:41:24 +08:00 · 1971 次点击
    这是一个创建于 1956 天前的主题,其中的信息可能已经有所发展或是发生改变。
    最近下载了一个 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 属性,该怎么办?
    4 条回复    2018-11-20 09:02:19 +08:00
    no1xsyzy
        1
    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
        2
    warcraft1236  
       2018-11-19 16:05:32 +08:00
    查了一下,pip install filetype 还是有的,而且文档中给了用法
    chenstack
        3
    chenstack  
       2018-11-19 17:04:58 +08:00
    试了一下,可以用 io.IOBase 判断
    from io import IOBase
    isinstance(r, IOBase)
    acone2003
        4
    acone2003  
    OP
       2018-11-20 09:02:19 +08:00
    谢谢 chenstack,OK!同时也谢谢楼上两位!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3558 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 04:55 · PVG 12:55 · LAX 21:55 · JFK 00:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.