pycharm 打开Path对象时出现Pychar类型提示警告

x759pob2  于 2023-01-20  发布在  PyCharm
关注(0)|答案(1)|浏览(150)

使用以下代码:

from pathlib import Path

file = Path("test.txt")

with open(file) as fl:
    pass

Pycharm在file上给出以下警告

Unexpected type(s): (Path) 

Possible types: 
  (Union[str, bytes, int]) 
  (Union[str, bytes, int, PathLike]) less... (Ctrl+F1) 

Inspection info: This inspection detects type errors in function call expressions. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Types of function parameters can be specified in docstrings or in Python 3 function annotations.

我做错什么了吗?

gajydyqb

gajydyqb1#

你可以这样使用它

file = "test.txt"

    with open(file) as fl:
        pass
  • Pycharm甚至会提示您可能要使用的文件。我这样使用它没有任何问题。
  • 如果您要查找的文件与您需要添加完整路径的代码不在同一目录中

相关问题