Warning

This documentation is for an old version of IPython. You can find docs for newer versions here.

Module: utils.path

Utilities for path handling.

1 Class

class IPython.utils.path.HomeDirError

Bases: exceptions.Exception

23 Functions

IPython.utils.path.get_long_path_name(path)

Expand a path into its long form.

On Windows this expands any ~ in the paths. On other platforms, it is a null operation.

IPython.utils.path.unquote_filename(name, win32=False)

On Windows, remove leading and trailing quotes from filenames.

IPython.utils.path.compress_user(path)

Reverse of os.path.expanduser()

IPython.utils.path.get_py_filename(name, force_win32=None)

Return a valid python filename in the current directory.

If the given name is not a file, it adds ‘.py’ and searches again. Raises IOError with an informative message if the file isn’t found.

On Windows, apply Windows semantics to the filename. In particular, remove any quoting that has been applied to it. This option can be forced for testing purposes.

IPython.utils.path.filefind(filename, path_dirs=None)

Find a file by looking through a sequence of paths.

This iterates through a sequence of paths looking for a file and returns the full, absolute path of the first occurence of the file. If no set of path dirs is given, the filename is tested as is, after running through expandvars() and expanduser(). Thus a simple call:

filefind('myfile.txt')

will find the file in the current working dir, but:

filefind('~/myfile.txt')

Will find the file in the users home directory. This function does not automatically try any paths, such as the cwd or the user’s home directory.

Parameters:

filename : str

The filename to look for.

path_dirs : str, None or sequence of str

The sequence of paths to look for the file in. If None, the filename need to be absolute or be in the cwd. If a string, the string is put into a sequence and the searched. If a sequence, walk through each element and join with filename, calling expandvars() and expanduser() before testing for existence.

Returns:

Raises IOError or returns absolute path to file.

IPython.utils.path.get_home_dir(require_writable=False)

Return the ‘home’ directory, as a unicode string.

Uses os.path.expanduser(‘~’), and checks for writability.

See stdlib docs for how this is determined. $HOME is first priority on ALL platforms.

Parameters:

require_writable : bool [default: False]

if True:

guarantees the return value is a writable directory, otherwise raises HomeDirError

if False:

The path is resolved, but it is not guaranteed to exist or be writable.

IPython.utils.path.get_xdg_dir()

Return the XDG_CONFIG_HOME, if it is defined and exists, else None.

This is only for non-OS X posix (Linux,Unix,etc.) systems.

IPython.utils.path.get_xdg_cache_dir()

Return the XDG_CACHE_HOME, if it is defined and exists, else None.

This is only for non-OS X posix (Linux,Unix,etc.) systems.

IPython.utils.path.get_ipython_dir()

Get the IPython directory for this platform and user.

This uses the logic in get_home_dir to find the home directory and then adds .ipython to the end of the path.

IPython.utils.path.get_ipython_cache_dir()

Get the cache directory it is created if it does not exist.

IPython.utils.path.get_ipython_package_dir()

Get the base directory where IPython itself is installed.

IPython.utils.path.get_ipython_module_path(module_str)

Find the path to an IPython module in this version of IPython.

This will always find the version of the module that is in this importable IPython package. This will always return the path to the .py version of the module.

IPython.utils.path.locate_profile(profile='default')

Find the path to the folder associated with a given profile.

I.e. find $IPYTHONDIR/profile_whatever.

IPython.utils.path.expand_path(s)

Expand $VARS and ~names in a string, like a shell

Examples:

In [2]: os.environ[‘FOO’]=’test’

In [3]: expand_path(‘variable FOO is $FOO’) Out[3]: ‘variable FOO is test’

IPython.utils.path.unescape_glob(string)

Unescape glob pattern in string.

IPython.utils.path.shellglob(args)

Do glob expansion for each element in args and return a flattened list.

Unmatched glob pattern will remain as-is in the returned list.

IPython.utils.path.target_outdated(target, deps)

Determine whether a target is out of date.

target_outdated(target,deps) -> 1/0

deps: list of filenames which MUST exist. target: single filename which may or may not exist.

If target doesn’t exist or is older than any file listed in deps, return true, otherwise return false.

IPython.utils.path.target_update(target, deps, cmd)

Update a target with a given command given a list of dependencies.

target_update(target,deps,cmd) -> runs cmd if target is outdated.

This is just a wrapper around target_outdated() which calls the given command if target is outdated.

IPython.utils.path.filehash(path)

Make an MD5 hash of a file, ignoring any differences in line ending characters.

IPython.utils.path.check_for_old_config(ipython_dir=None)

Check for old config files, and present a warning if they exist.

A link to the docs of the new config is included in the message.

This should mitigate confusion with the transition to the new config system in 0.11.

IPython.utils.path.get_security_file(filename, profile='default')

Return the absolute path of a security file given by filename and profile

This allows users and developers to find security files without knowledge of the IPython directory structure. The search path will be [‘.’, profile.security_dir]

Parameters:

filename : str

The file to be found. If it is passed as an absolute path, it will simply be returned.

profile : str [default: ‘default’]

The name of the profile to search. Leaving this unspecified The file to be found. If it is passed as an absolute path, fname will simply be returned.

Returns:

Raises IOError if file not found or returns absolute path to file.

Hard links src to dst, returning 0 or errno.

Note that the special errno ENOLINK will be returned if os.link isn’t supported by the operating system.

Attempts to hardlink src to dst, copying if the link fails.

Attempts to maintain the semantics of shutil.copy.

Because os.link does not overwrite files, a unique temporary file will be used if the target already exists, then that file will be moved into place.