Python - When to use file vs open

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

    Functionally, the two are the same; open will call file anyway, so currently the difference is a matter of style. The Python docs recommend using open.

    When opening a file, it's preferable to use open() instead of invoking the file constructor directly.

    The reason is that in future versions they is not guaranteed to be the same (open will become a factory function, which returns objects of different types depending on the path it's opening).

提交回复
热议问题