Python中file()和open()的区别?
PHPz
PHPz 2017-04-17 14:17:32
[Python讨论组]

Python里file()和open()有什么区别?
现在自己知道的区别有file()不能创建文件,open()可以。
还有其他区别吗?

PHPz
PHPz

学习是最好的投资!

全部回复(2)
PHP中文网

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 也是能够创建文件的。

高洛峰

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

>>> 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)

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号