扫码关注官方订阅号
Python里file()和open()有什么区别? 现在自己知道的区别有file()不能创建文件,open()可以。 还有其他区别吗?
学习是最好的投资!
Python 2 里基本没区别。Python 3 里没 file。
file
Python 2 里,file 是文件对象。open 是返回新创建的文件对象的内建函数,相当于:
open
pythondef open(*args, **kwargs): return file(*args, **kwargs)
python
def 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); }
c
static 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)
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
Python 2 里基本没区别。Python 3 里没
file。Python 2 里,
file是文件对象。open是返回新创建的文件对象的内建函数,相当于:它真实的定义是:
所以
file也是能够创建文件的。没有特别的区别吧, 查看他们的文档