thnkwthprtls
thnkwthprtls

Reputation: 3487

Python (and possibly others) - What is the escape character for a space?

Pretty simple concept, but I can't figure it out and could use some help. I need to check if a file in my Program Files directory exists, so I have the following:

import os

if not os.path.exists('C:/Program Files/file_to_be_found'):
  print "ERROR: Not Found!"
else:
  #rest of program...

However I know I can't do it this way. How can I write the path in the command to accept the space between "Program" and "Files"?

Upvotes: 1

Views: 1037

Answers (1)

Bor
Bor

Reputation: 189

the space is ok. and you are free to write the slash.

if os.path.exists('C:/Program Files'): print 'yes'
if os.path.exists(r'C:\Program Files'): print 'yes'
if os.path.exists('C:\\Program Files'): print 'yes'

all above are ok with or without a "r".

@nneonneo reminded that the second one is dangerous without "r" because the backslash is used to escape.

Upvotes: 3

Related Questions