Python-何时使用文件vs打开

问题:Python-何时使用文件vs打开

fileopenPython 和有什么不一样?我什么时候应该使用哪个?(假设我处于2.5级)

What’s the difference between file and open in Python? When should I use which one? (Say I’m in 2.5)


回答 0

您应该始终使用open()

文档所述:

打开文件时,最好使用open()而不是直接调用此构造函数。文件更适合类型测试(例如,编写“ isinstance(f,file)”)。

另外,自Python 3.0起file() 已被删除

You should always use open().

As the documentation states:

When opening a file, it’s preferable to use open() instead of invoking this constructor directly. file is more suited to type testing (for example, writing “isinstance(f, file)”).

Also, file() has been removed since Python 3.0.


回答 1

原因有两个:python哲学“应该有一种实现方法”并且file正在消失。

file是实际类型(使用例如file('myfile.txt')调用其构造函数)。open是工厂函数,它将返回文件对象。

在python 3.0 file中,它将从内置变为由io库中的多个类实现(有点类似于带有缓冲读取器的Java等)。

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


回答 2

file()是一种类型,例如int或列表。open()是用于打开文件的函数,它将返回一个file对象。

这是何时应使用open的示例:

f = open(filename, 'r')
for line in f:
    process(line)
f.close()

这是何时应使用文件的示例:

class LoggingFile(file):
    def write(self, data):
        sys.stderr.write("Wrote %d bytes\n" % len(data))
        super(LoggingFile, self).write(data)

如您所见,存在两者的充分理由和明确的用例。

file() is a type, like an int or a list. open() is a function for opening files, and will return a file object.

This is an example of when you should use open:

f = open(filename, 'r')
for line in f:
    process(line)
f.close()

This is an example of when you should use file:

class LoggingFile(file):
    def write(self, data):
        sys.stderr.write("Wrote %d bytes\n" % len(data))
        super(LoggingFile, self).write(data)

As you can see, there’s a good reason for both to exist, and a clear use-case for both.


回答 3

在功能上,两者是相同的;无论如何open都会调用file,因此当前的区别在于样式。在Python文档建议使用open

打开文件时,最好使用open()而不是直接调用文件构造函数。

原因是在将来的版本中不能保证它们是相同的(open将成为工厂函数,根据其打开的路径返回不同类型的对象)。

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


回答 4

仅使用open()打开文件。file()实际上在3.0中已被删除,目前不推荐使用。他们之间有一种奇怪的关系,但是file()现在正在进行中,因此不再需要担心。

以下来自Python 2.6文档。[括号内的内容]由我添加。

打开文件时,最好使用open()而不是直接调用此[file()]构造函数。文件更适合类型测试(例如,编写isinstance(f,file)

Only ever use open() for opening files. file() is actually being removed in 3.0, and it’s deprecated at the moment. They’ve had a sort of strange relationship, but file() is going now, so there’s no need to worry anymore.

The following is from the Python 2.6 docs. [bracket stuff] added by me.

When opening a file, it’s preferable to use open() instead of invoking this [file()] constructor directly. file is more suited to type testing (for example, writing isinstance(f, file)


回答 5

Van Rossum先生说,尽管open()当前是file()的别名,但您应该使用open(),因为将来可能会改变。

According to Mr Van Rossum, although open() is currently an alias for file() you should use open() because this might change in the future.