Generic way to check validity of file name in Python?

Question:

Creating a file can fail in Python because of various file name related reasons:

  1. the file path is too long (see e.g.: what-is-the-maximum-length-of-a-file-path-in-ubuntu )

  2. the file name may be too long, as the file system is encrypted – see e.g. comment in answer to that question:

    On encrypted filesystems the max filename length is 143 bytes. To decide whether a filename is short enough you can find his byte length in Python with len(filename.encode()). – Marvo, Mar 9, 2018 at 12:45

  3. there are characters that either make issues in the file system, are not recommended or could make issues in another file system like:  n!@#$%^&*()[]{};:,/<>?|`~=+

  4. … .

Is there any convenience function that tells me beforehand whether

  • a) my file name will work
  • b) may lead to issues in other file systems
  • c) and why it will fail?

Solutions like:
Check whether a path is valid in Python without creating a file at the path's target
or
Validate a filename in python

unfortunately do not fulfill the requirements a) – c) (even the first one does not recognize the 143 character restrictions for encrypted folders / drives)

Asked By: user7468395

||

Answers:

I’m generally tempted to create very restrictive rules initially and back them off if there’s need .. for a specialized filesystem which has different features and requirements to what Python naturally supports, it’s likely you’ll need to read the documentation, experiment, and write the rules yourself.

That said, try it and see could be fine and give you much better functionality than you would want to write or support (for example, should you test handling for various PermissionError cases or the plethora of network filesytems?), and you can case Exceptions more than once to give a better error message or handling (note only the first match is chosen, so put less-generic, more-inherited Exceptions earlier)

try:
    # attempt to create file
except IsADirectoryError:
    raise CustomException("did you mean to target a directory?")
except OSError as ex:
    raise CustomException("improper file") from ex
    ...
except Exception as ex:
    raise CustomException("unexpected Exception") from ex
else:  # did not raise
    # extra opportunity to verify the file was actually written

Refer to the Exception Hierarchy tree for what you might expect or want to handle before a generic Exception https://docs.python.org/3/library/exceptions.html#exception-hierarchy

Answered By: ti7

If external package are allowed I suggest you try pathvalidate, function validate_filename should be of interest if you need to find why it will fail? whilst is_valid_filename if you need to find if file name will work

Answered By: Daweo
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.