Check if string is valid filename in Python?

I need to make sure that a user-provided string is permissible as a filename on my system. To do this, I need to replace any forbidden characters with ‘_’. The list of forbidden characters varies by OS, so I am wondering if there is already a function available that takes this into account.

Using str.replace() is an option, but I am looking for something that is already available.

You can use the os.path.normpath() function to normalize the path, which will replace any forbidden characters with ‘_’ according to the specific OS’s rules.