Python - When to use file vs open

PythonFile

Python Problem Overview


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

Python Solutions


Solution 1 - Python

You should always use open().

As the [documentation][1] 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][2] since Python 3.0.

[1]: http://docs.python.org/2/library/functions.html#file "BuiltinFuncs Doc" [2]: http://docs.python.org/release/3.0/whatsnew/3.0.html#builtins

Solution 2 - Python

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

Solution 3 - Python

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.

Solution 4 - Python

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

Solution 5 - Python

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)

Solution 6 - Python

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGregView Question on Stackoverflow
Solution 1 - PythonnoskloView Answer on Stackoverflow
Solution 2 - PythonRyanView Answer on Stackoverflow
Solution 3 - PythonJerubView Answer on Stackoverflow
Solution 4 - PythondF.View Answer on Stackoverflow
Solution 5 - PythonDevin JeanpierreView Answer on Stackoverflow
Solution 6 - PythonMartin BeckettView Answer on Stackoverflow