为了账号安全,请及时绑定邮箱和手机立即绑定

Python中file()和open()的区别?

Python中file()和open()的区别?

慕的地6264312 2019-02-25 20:53:33
Python里file()和open()有什么区别? 现在自己知道的区别有file()不能创建文件,open()可以。 还有其他区别吗?
查看完整描述

2 回答

?
回首忆惘然

TA贡献1847条经验 获得超11个赞

Python 2 里基本没区别。Python 3 里没 file

Python 2 里,file 是文件对象。open 是返回新创建的文件对象的内建函数,相当于:

pythondef open(*args, **kwargs):
  return file(*args, **kwargs)

它真实的定义是:

cstatic PyObject *
builtin_open(PyObject *self, PyObject *args, PyObject *kwds)
{
    return PyObject_Call((PyObject*)&PyFile_Type, args, kwds);
}

所以 file 也是能够创建文件的。

查看完整回答
反对 回复 2019-03-01
?
jeck猫

TA贡献1909条经验 获得超7个赞

没有特别的区别吧, 查看他们的文档

>>> help(open)

open(...)
    open(name[, mode[, buffering]]) -> file object

    Open a file using the file() type, returns a file object.  This is the
    preferred way to open a file.  See file.__doc__ for further information.
(END)

查看完整回答
反对 回复 2019-03-01
  • 2 回答
  • 0 关注
  • 901 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信