Python file() function Python file() function python-3.x python-3.x

Python file() function


In Python 2, open and file are mostly equivalent. file is the type and open is a function with a slightly friendlier name; both take the same arguments and do the same thing when called, but calling file to create files is discouraged and trying to do type checks with isinstance(thing, open) doesn't work.

In Python 3, the file implementation in the io module is the default, and the file type in the builtin namespace is gone. open still works, and is what you should use.


You can do what the documentation for file() suggests -

When opening a file, it’s preferable to use open() instead of invoking this constructor directly.

You should use open() method instead.