user1800967
user1800967

Reputation: 955

using os.scandir() results in 'unresolved attribute reference' warnings in pycharm

I have the following python code in pycharm:

# search for files or folders with two or more adjacent spaces
def twoplusspaces(path):

    srchr = os.scandir(path) # get our iterator

    # iterate through folders and files looking for adjacent blank spaces
    for entry in srchr:
        if "  " in entry.name:
            print(entry.name) # print name of file/folder with 2+ spaces
        if entry.is_dir():
            twoplusspaces(path + "\\" + entry.name) # recursive call when folder encountered

    srchr.close() # done using iterator

This works fine as intended but pycharm is warning me 'unresolved attribute reference' for entry.name and entry.is_dir(). I find this odd because Python documentation says these attributes exist for the DirEntry object that is returned by scandir(): https://docs.python.org/3/library/os.html#os.DirEntry

Looks like there might be an issue with my pycharm but not sure... Any help or elaboration on this matter is appreciated, thanks!

Upvotes: 5

Views: 854

Answers (2)

Samuele Darani
Samuele Darani

Reputation: 1

A way to get around this problem (not to fix it though) is to add

assert isinstance(entry, os.DirEntry)

right after the for loop. This way the IDE knows you are going to use it as such.

Upvotes: 0

user1800967
user1800967

Reputation: 955

Looks like this is a bug with pycharm. Will ignore for now then. Thanks Steve!

Upvotes: 2

Related Questions