Python - When to use file vs open

前端 未结 6 1831
一个人的身影
一个人的身影 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:26

    You should always use open().

    As the documentation states:

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

    Also, file() has been removed since Python 3.0.

提交回复
热议问题