Receiving AttributeError from os.path.isfile() function

19,585

Solution 1

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.

Solution 2

Try

import os.path

instead

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

Share:
19,585
WR7500
Author by

WR7500

Updated on June 28, 2022

Comments

  • WR7500
    WR7500 almost 2 years

    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
    WR7500 almost 10 years
    print os.path gives me <module 'ntpath' from 'C:\Python27\lib\ntpath.pyc'>. Not sure what that is.
  • mgilson
    mgilson almost 10 years
    @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).