141

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

mpen
  • 272,448
  • 266
  • 850
  • 1,236
Greg
  • 45,306
  • 89
  • 231
  • 297

6 Answers6

157

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.

ElementW
  • 804
  • 8
  • 23
nosklo
  • 217,122
  • 57
  • 293
  • 297
  • 23
    This is distressing, since the documentation used to state "The file() constructor is new in Python 2.2. The previous spelling, open(), is retained for compatibility, and is an alias for file()." Since I decided to RTFM ten years ago, and became very fond the the unification of types and classes, I never used open() again. Moreover, I still feel that the type constructor is the more obvious way to return a file like object, located by the path arg, and behaving as requested in the mode arg. I especially feel this way when the obvious intent of the devs back then was 2 retain open4compat. – umeboshi Sep 21 '13 at 17:35
33

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

Ryan
  • 15,016
  • 6
  • 48
  • 50
19

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.

Jerub
  • 41,746
  • 15
  • 73
  • 90
  • 5
    Generally, files should be opened with the `with` statement. `with open(filename, 'r') as f: \ for line in f: \ process(line)`. This avoids the explicit close. Python 2.6 and above natively support the `with` statement. In Python 2.5, you must add `from __future__ import with_statement` to the top of your code. – IceArdor Jul 14 '14 at 20:40
  • Didn't you just redefine the built-in name in the second example? – planetp Oct 13 '14 at 11:44
  • 1
    @planetp, the second example just inherits from the file class. I suppose it's weird that it's in lower case, but that's how it is with the basic builtin types (e.g. object, str, list, ...) – yoniLavi Oct 23 '14 at 15:15
  • 1
    In Python 2.5, the `file` class was equipped with special methods that are automatically called whenever a file is opened via a `with` statement. These special methods ensure that the file is properly and safely opened and closed. – Mausy5043 Feb 06 '16 at 10:11
7

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

dF.
  • 74,139
  • 30
  • 130
  • 136
  • 2
    open is already a factory function, so there are functional differences (eg. inheritance, isinstance() etc) – Brian Sep 22 '08 at 07:53
4

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)

Devin Jeanpierre
  • 92,913
  • 4
  • 55
  • 79
2

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

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263