2

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)

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

this is the correct approach. also, i would add OSError as that is the expected error from a filename that exceeds the max length size iirc OSError: [Errno 22] Invalid argument on a file name when trying to open it
at the lack of an operating system feature to let you directly check just that, this is the best we can do. not very perfect as in some scenarios you may want to delete the file right away and then wait till your program really needs to write it, and that gives rise to race conditions in concurrent code, making your design for this kind of safety a little more elaborate than you'd want to, but doable.
1

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

2 Comments

the 143 characters limit is not (yet) fulfilled with this library, but at least the comparison sanitized name vs unsanitized name helps to find the erroneous character(s) quickly.
kind of a missing OS feature, that would avoid the need for any library predicting file name validity to the extent possible. or just, forget about using dynamically generated file names which are based on arbitrary program inputs and design your persistence strategy to avoid the need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.