Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

os.path.isfile() returns false for existing Windows file

Tags:

python

For some reason os.path.isfile() occasionally returns false for some existing Windows files. At first, I assumed that spaces in the filename were causing a problem, but other file paths with spaces worked fine. Here's copy from the Python console that illustrates this issue:

>>> import os
>>> os.path.isfile("C:\Program Files\Internet Explorer\images\bing.ico")
False
>>> os.path.isfile("C:\Program Files\Internet Explorer\images\PinnedSiteLogo.contrast-black_scale-80.png")
True

How can I fix this problem?

like image 833
Nemo XXX Avatar asked Apr 25 '15 09:04

Nemo XXX


1 Answers

\b in a string means backspace. If you want actual backslashes in a string, they need to be escaped with more backslashes (\\ instead of \), or you need to use a raw string (r"..." instead of "..."). For file paths, I'd recommend using forward slashes.

like image 173
user2357112 supports Monica Avatar answered Sep 27 '22 22:09

user2357112 supports Monica