Python - When to use file vs open

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

    Two reasons: The python philosophy of "There ought to be one way to do it" and file is going away.

    file is the actual type (using e.g. file('myfile.txt') is calling its constructor). open is a factory function that will return a file object.

    In python 3.0 file is going to move from being a built-in to being implemented by multiple classes in the io library (somewhat similar to Java with buffered readers, etc.)

提交回复
热议问题