Creating a file can fail in Python because of various file name related reasons:
-
the file path is too long (see e.g.: what-is-the-maximum-length-of-a-file-path-in-ubuntu )
-
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 -
there are characters that either make issues in the file system, are not recommended or could make issues in another file system like:
n!@#$%^&*()[]{};:,/<>?|`~=+
-
… .
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
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)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-hierarchyIf 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? whilstis_valid_filename
if you need to find if file name will work