Validating destination file paths

Rated 4.78/5 based on 857 customer reviews

try: if not isinstance(pathname, str) or not pathname: return False # Strip this pathname's Windows-specific drive specifier (e.g., `C:\`) # if any.

Since Windows prohibits path components from containing `:` # characters, failing to strip this `:`-suffixed prefix would # erroneously invalidate all valid absolute Windows pathnames.

Under Windows, "winerror" is more # fine-grained and hence useful than the generic "errno" # attribute.

When a too-long pathname is passed, for example, # "errno" is "ENOENT" (i.e., no such file or directory) rather # than "ENAMETOOLONG" (i.e., file name too long).

Does this mean that pathnames residing in non-existing directories are validatable?

This function is guaranteed to _never_ raise exceptions.

''' try: # To prevent "os" module calls from raising undesirable exceptions on # invalid pathnames, is_pathname_valid() is explicitly called first.

The question of "How do I test pathname validity and, for valid pathnames, the existence or writability of those paths? Both are interesting, and neither have received a genuinely satisfactory answer here... Before hurling our fragile meat suits into the python-riddled moshpits of pain, we should probably define what we mean by "pathname validity." What defines validity, exactly?

By "root filesystem," we mean: exceptions when passed pathnames residing in non-existing directories, regardless of whether those pathnames are invalid or not.

Leave a Reply