4

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.

WR7500
  • 417
  • 1
  • 3
  • 12

3 Answers3

3

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.

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • `print os.path` gives me ``. Not sure what that is. – WR7500 May 09 '14 at 20:29
  • @user3492006 -- that makes no sense with the AttributeError you posted. The attributeError says that a __'function'__ has no attribute `'isfile'`, but you're saying that printing `os.path` does in fact return a `module` (which should have `isfile` defined within). – mgilson May 09 '14 at 20:31
1

Try

import os.path

instead

see this thread for more info: How do I check whether a file exists using Python?

Community
  • 1
  • 1
Elias
  • 1,367
  • 11
  • 25
0

Found the issue. I had an if/else statement earlier in the code, which was being used to gather the OS version the script was running on. Turns out I used OS (caps) for the variable name, which I think caused this. I changed it around, and it's fixed.

WR7500
  • 417
  • 1
  • 3
  • 12