0xad 0xad - 1 year ago 70
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:


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:


As such it does open in bash (e.g.
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
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

At this point I'm slightly out of ideas.

Answer Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download