pysys.utils.fileutils¶
File and directory handling utility functions such as mkdir and deletedir, with enhanced error handling and support for long paths on Windows.
toLongPathSafe¶
-
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.
- Parameters
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.
- Returns
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.
fromLongPathSafe¶
-
pysys.utils.fileutils.
fromLongPathSafe
(path)[source]¶ Strip off
\?\
prefixes added bytoLongPathSafe
.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())
.
pathexists¶
-
pysys.utils.fileutils.
pathexists
(path)[source]¶ 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.
- Parameters
path – If None or empty, returns True. Only Python 2, can be a unicode or byte string.
mkdir¶
deletedir¶
-
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.- Parameters
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.