File and directory handling utility functions such as mkdir and deletedir, with enhanced error handling and support for long paths on Windows. Also some simple utilities for loading properties and JSON files.


pysys.utils.fileutils.toLongPathSafe(path, onlyIfNeeded=False)[source]

Converts the specified path string to a form suitable for passing to API calls if it exceeds the maximum path length on this OS.

Currently, this is necessary only on Windows, where a unicode string starting with ?must be used to get correct behaviour for long paths. On Windows this function also normalizes the capitalization of drive letters so they are always upper case regardless of OS version and current working directory.

  • path – A path. Must not be a relative path. Can be None/empty. Can contain “..” sequences. If possible, use a unicode character string. On Python 2, byte strings are permitted and converted using locale.getpreferredencoding().

  • onlyIfNeeded – Set to True to only adds the long path support if this path exceeds the maximum length on this OS (e.g. 256 chars). You must keep this at False if you will be adding extra characters on to the end of the returned string.


The passed-in path, possibly with a \?\ prefix added, forward slashes converted to backslashes on Windows, and converted to a unicode string. Trailing slashes may be removed. Note that the conversion to unicode requires a lot of care on Python 2 where byte strings are more common, since it is not possible to combine unicode and byte strings (if they have non-ascii characters), for example for a log statement.



Strip off \?\ prefixes added by toLongPathSafe.

Note that this function does not convert unicode strings back to byte strings, so if you want a complete reversal of toLongPathSafe you will additionally have to call result.encode(locale.getpreferredencoding()).



Returns True if the specified path is an existing file or directory, as returned by os.path.exists.

This method is safe to call on paths that may be over the Windows 256 character limit.


path – If None or empty, returns True. Only Python 2, can be a unicode or byte string.



Create a directory, with recursive creation of any parent directories.

This function is a no-op (does not throw) if the directory already exists.


Returns the path passed in.


pysys.utils.fileutils.deletedir(path, retries=1, ignore_errors=False, onerror=None)[source]

Recursively delete the specified directory, with optional retries.

Does nothing if it does not exist. Raises an exception if the deletion fails (unless onerror= is specified), but deletes as many files as possible before doing so.

  • retries – The number of retries to attempt. This can be useful to work around temporary failures causes by Windows file locking.

  • ignore_errors – If True, an exception is raised if the path exists but cannot be deleted.

  • onerror – A callable that with arguments (function, path, excinfo), called when an error occurs while deleting. See the documentation for shutil.rmtree for more information.


pysys.utils.fileutils.loadProperties(path, encoding='utf-8-sig')[source]

Reads keys and values from the specified .properties file.

Support # and ! comments but does not perform any special handling of backslash \ characters (either for escaping or for line continuation). Leading and trailing whitespace around keys and values is stripped. If you need handling of \ escapes and/or expansion of ${...} placeholders this should be performed manually on the returned values.

  • path (str) – The absolute path to the properties file.

  • encoding (str) – The encoding to use (unless running under Python 2 in which case byte strings are always returned). The default is UTF-8 (with optional Byte Order Mark).

Return dict[str:str]

An ordered dictionary containing the keys and values from the file.


pysys.utils.fileutils.loadJSON(path, **kwargs)[source]

Reads JSON from the specified path.

This is a small wrapper around Python’s json.load() function.

  • path (str) – The absolute path to the JSON file, which must be encoded using UTF-8 (with optional Byte Order Mark).

  • kwargs – Keyword arguments will be passed to json.load().

Return obj

A dict, list, or other Python object representing the contents of the JSON file.