Receiving AttributeError from os.path.isfile() function

WR7500 picture WR7500 · May 9, 2014 · Viewed 17.8k times · Source

Receiving the below error when running my script:

Traceback (most recent call last):
  File "HC_Main.py", line 54, in <module>
    setup_exists = os.path.isfile(config_file)
AttributeError: 'function' object has no attribute 'isfile'

Sample code is:

import os
setup_exists = os.path.isfile(setup_exists)
if setup_exists is False:
    print "Setup file exists"

When I checked the IDLE console with dir(os.path), isfile is listed. Additionaly, I can use the function without issues in IDLE as well.

Could it be my IDE causing issues here? I've also tried running the script apart from the IDE, but it still receives the error.

Answer

mgilson picture mgilson · May 9, 2014

Somehow, os.path is no longer the builtin module, but it has been replaced with a function. Check your code to make sure you didn't accidentally monkey-patch it somewhere.

For clues, you could start by putting:

print os.path

right before the line where you actually use os.path.isfile. This should give you the function's name which will hopefully give you a good place to start looking.