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:45there 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)