Python - When to use file vs open

前端 未结 6 1839
一个人的身影
一个人的身影 2020-11-27 02:43

What\'s the difference between file and open in Python? When should I use which one? (Say I\'m in 2.5)

6条回答
  •  日久生厌
    2020-11-27 03:36

    Only ever use open() for opening files. file() is actually being removed in 3.0, and it's deprecated at the moment. They've had a sort of strange relationship, but file() is going now, so there's no need to worry anymore.

    The following is from the Python 2.6 docs. [bracket stuff] added by me.

    When opening a file, it’s preferable to use open() instead of invoking this [file()] constructor directly. file is more suited to type testing (for example, writing isinstance(f, file)

提交回复
热议问题