Validating destination file paths
try: if not isinstance(pathname, str) or not pathname: return False # Strip this pathname's Windows-specific drive specifier (e.g., `C:\`) # if any.
# # Only the following exceptions indicate invalid pathnames: # # * Instances of the Windows-specific "Windows Error" class # defining the "winerror" attribute whose value is # "ERROR_INVALID_NAME".
Malicious users may attempt to repeatedly validate pathnames residing on filesystems known to be stale or otherwise slow (e.g., NFS Samba shares); in that case, blindly statting incoming pathnames is liable to either eventually fail with connection timeouts or consume more time and resources than your feeble capacity to withstand unemployment.
import errno, os # Sadly, Python fails to provide the following magic number for us.
''' def is_pathname_valid(pathname: str) - bool: ''' `True` if the passed pathname is a valid pathname for the current OS; `False` otherwise.
''' # If this pathname is either not a string or is but is empty, this pathname # is invalid.