very-cool-group

open

The built-in function open() is one of several ways to open files on your computer. It accepts many different parameters, so this tag will only go over two of them (file and mode). For more extensive documentation on all these parameters, consult the official documentation. The object returned from this function is a file object or stream, for which the full documentation can be found here.

See also:
- !tags with for information on context managers
- !tags pathlib for an alternative way of opening files
- !tags seek for information on changing your position in a file

The file parameter

This should be a path-like object denoting the name or path (absolute or relative) to the file you want to open.

An absolute path is the full path from your root directory to the file you want to open. Generally this is the option you should choose so it doesn't matter what directory you're in when you execute your module.

See !tags relative-path for more information on relative paths.

The mode parameter

This is an optional string that specifies the mode in which the file should be opened. There's not enough room to discuss them all, but listed below are some of the more confusing modes.

  • 'r+' Opens for reading and writing (file must already exist)
  • 'w+' Opens for reading and writing and truncates (can create files)
  • 'x' Creates file and opens for writing (file must not already exist)
  • 'x+' Creates file and opens for reading and writing (file must not already exist)
  • 'a+' Opens file for reading and writing at end of file (can create files)
star-imports

Wildcard imports are import statements in the form from <module_name> import *. What imports like these do is that they import everything [1] from the module into the current module's namespace [2]. This allows you to use names defined in the imported module without prefixing the module's name.

Example:

>>> from math import *
>>> sin(pi / 2)
1.0
This is discouraged, for various reasons:

Example:

>>> from custom_sin import sin
>>> from math import *
>>> sin(pi / 2)  # uses sin from math rather than your custom sin
- Potential namespace collision. Names defined from a previous import might get shadowed by a wildcard import. - Causes ambiguity. From the example, it is unclear which sin function is actually being used. From the Zen of Python [3]: Explicit is better than implicit. - Makes import order significant, which they shouldn't. Certain IDE's sort import functionality may end up breaking code due to namespace collision.

How should you import?

  • Import the module under the module's namespace (Only import the name of the module, and names defined in the module can be used by prefixing the module's name)
    >>> import math
    >>> math.sin(math.pi / 2)
  • Explicitly import certain names from the module
    >>> from math import sin, pi
    >>> sin(pi / 2)
    Conclusion: Namespaces are one honking great idea -- let's do more of those! [3]

[1] If the module defines the variable __all__, the names defined in __all__ will get imported by the wildcard import, otherwise all the names in the module get imported (except for names with a leading underscore) [2] Namespaces and scopes [3] Zen of Python