0xad 0xad - 3 months ago 12
Bash Question

Special shell characters when opening in a file Python

I have an interesting problem that I couldn't solve by googling.

I'm trying to process each file inside of the directory. Some of the file names include special shell characters (

$
,
(
,
)
etc). A good example of such file name:

FOO.-6.BAR.(nil).$0BAZ


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

FOO.-6.BAR.\(nil\).\$0BAZ


As such it does open in bash (e.g.
cat
ing works).

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

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

At this point I'm slightly out of ideas.

Answer

You don't need to escape anything when opening the file from Python, because you aren't executing a shell command at all.

f = open("FOO.-6.BAR.(nil).$0BAZ")

The same is true of any file name returned by os.listdir:

for fname in os.listdir('.'):
    f = open(fname)