Special shell characters when opening in a file Python
<p>I have an interesting problem that I couldn't solve by googling.</p>

<p>I'm trying to process each file inside of the directory. Some of the file names include special shell characters (<code>$</code>, <code>(</code>, <code>)</code> etc). A good example of such file name:</p>

<p><code>FOO.-6.BAR.(nil).$0BAZ</code></p>

<p>When I try to open this file in bash without any escaping I get an error <code>No matches found</code> therefore I <strong>need</strong> to escape it in a following fashion:</p>

<p><code>FOO.-6.BAR.\(nil\).\$0BAZ</code></p>

<p>As such it does open in bash (e.g. <code>cat</code>ing works).</p>

<p>However, the problem is that even when I escape the file name in exact same fashion in python's code I still get <code>IOError: [Errno 2] No such file or directory</code>. </p>

<p>I've also noticed that when I escape special characters (e.g. <code>f.replace('$', '\$')</code>) the final file name used in <code>open</code> is double prepending my slashes (<code>IOError: [Errno 2] No such file or directory: 'FOO.-6.BAR.\\(nil\\).\\$0BAZ'</code>) even though when I print the file name by hand (<code>print f</code>) I get valid <code>FOO.-6.BAR.\(nil\).\$0BAZ</code>.</p>

<p>At this point I'm slightly out of ideas.</p>