Pham Huu Bang
Pham Huu Bang

Reputation: 105

Handling a literal space in a filename

I have problem with os.access(filename, os.R_OK) when file is an absolute path on a Linux system with space in the filename. I have tried many ways of quoting the space, from "'" + filename + "'" to filename.replace(' ', '\\ ') but it doesn't work.

How can I escape the filename so my shell knows how to access it? In terminal I would address it as '/home/abc/LC\ 1.a'

Upvotes: 3

Views: 5165

Answers (1)

Chad S.
Chad S.

Reputation: 6633

You don't need to (and shouldn't) escape the space in the file name. When you are working with a command line shell, you need to escape the space because that's how the shell tokenizes the command and its arguments. Python, however, is expecting a file name, so if the file name has a space, you just include the space.

Upvotes: 6

Related Questions