首页 > 解决方案 > 使用 os.path.join() 运行脚本时出现 TypeError

问题描述

我正在运行的脚本中有以下几行:

api_xml = os.path.join(opts.out, os.path.basename(
    opts.api_raw).replace('.raw', '.xml'))

使用 Python 3.7 运行时,出现错误:

Traceback (most recent call last):
  File "generate_code.py", line 32, in <module>
    opts.api_raw).replace('.raw', '.xml'))
  File "/usr/lib/python3.7/posixpath.py", line 146, in basename
    p = os.fspath(p)
TypeError: expected str, bytes or os.PathLike object, not NoneType

在我看来,这就像一个简单的加入然后替换,不知道为什么会失败。

标签: pythonpython-3.xwindows

解决方案


TypeError: expected str, bytes or os.PathLike object, not NoneType

意味着您传递None给需要路径的函数。

在尝试构建之前尝试添加这些行api_xml

assert opts.out is not None
assert opts.api_raw is not None

推荐阅读