Remove all the parentheses and replace all spaces by underscores?
<p>Here is what I did:</p>

<pre><code>import re
def demicrosoft (fn):

    fn = re.sub('[()]', '', fn)
    for ch in [' ']:
        fn = fn.replace(ch,"_"+ch)
    return fn
print(demicrosoft('a bad file name (really)')) 
</code></pre>

<p><br></p>

<pre><code>&gt;&gt;&gt; (executing lines 1 to 12 of "&lt;tmp 2&gt;")
a_ bad_ file_ name_ really
</code></pre>

<p>There are spaces followed with the underscores. How can I fix it? </p>

<p>You can do this with <code>str.translate()</code>:</p>

<pre><code>&gt;&gt;&gt; table = str.maketrans({'(':None, ')':None, ' ':'_'})
&gt;&gt;&gt; 'a bad file name (really)'.translate(table)
'a_bad_file_name_really'
</code></pre>

python string replace