Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does python os.path.isfile return false on windows, for only a specific directory?

On windows 7, when I run this Python 2.7 code, "NOT file" prints, but the file is there, its not read only, the folder is not read only, none of its parents are read only.

if os.path.exists('D:\testfiles\mysub\GraphiteController.js'):
    print "IS file"
else:
    print "NOT file"
sys.exit(1)

If I move the file to d:\myother directory, prints "IS file". If I move the file to d:\testfiles directory, prints "NOT file".

I tried this on another windows machine, same problem. Very strange.

like image 960
Greg Lafrance Avatar asked Jan 11 '23 06:01

Greg Lafrance


1 Answers

It's because '\t' is a tab character. Use forward slashes in your paths or use raw strings:

if os.path.exists('D:/testfiles/mysub/GraphiteController.js'):

or

if os.path.exists(r'D:\testfiles\mysub\GraphiteController.js'):
like image 97
Wooble Avatar answered Feb 16 '23 23:02

Wooble