使用 os.scandir() 函数在 PyCharm 中会产生“未解决的属性引用”警告。

5

我在 PyCharm 中有以下的 Python 代码:

# 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

这个代码按照预期正常工作,但是PyCharm警告我entry.name和entry.is_dir()有'未解决的属性引用(unresolved attribute reference)'。我觉得这很奇怪,因为Python文档说scandir()返回的DirEntry对象具有这些属性:https://docs.python.org/3/library/os.html#os.DirEntry
看起来可能是PyCharm出了问题,但我不确定...... 如果有任何帮助或关于此问题的详细说明,将不胜感激,谢谢!

1
这不是你的设置问题。我也遇到了同样的情况。 - CryptoFool
4
这是一个已知的问题。请参阅https://youtrack.jetbrains.com/issue/PY-46041和https://youtrack.jetbrains.com/issue/PY-46048。 - CryptoFool
你也在PyCharm中尝试过这个吗?还是用了其他的IDE? - user1800967
1
是的,我正在使用PyCharm 2020.3.2,这应该是最新版本。 - CryptoFool
这是Pycharm的一个bug。我明白了,谢谢你提供的信息! - user1800967
2个回答

2

看起来这是PyCharm的一个bug。现在先忽略它。感谢Steve!


0
解决这个问题的一种方法(虽然不是修复它)是在for循环之后添加以下代码:

assert isinstance(entry, os.DirEntry)

这样IDE就知道你将要使用它。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接