Python AttributeError: 'NoneType' 对象没有属性 'endswith'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17772908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:09:34  来源:igfitidea点击:

AttributeError: 'NoneType' object has no attribute 'endswith'

pythonattributeerrornonetypeends-with

提问by mr.G

I am currently working on a Python script that updates a web page. But running the main script generates this error:

我目前正在开发一个更新网页的 Python 脚本。但是运行主脚本会产生这个错误:

<res status='-1'><error message="'NoneType' object has no attribute 'endswith'"><![CDATA[
Traceback (most recent call last):
  File "/path/to/file/ws_config.py", line XXXX, in Run
    tests = TestList().tests
  File "/path/to/file/ws_config.py", line XXXX, in __init__
    UpdateTestGroup(None),
  File "/path/to/file/ws_config.py", line XXXX, in __init__
    test = CT.CurlTest(settings),
  File "/path/to/file/config_tests.py", line XXXX, in __init__
    self.params.path = os.path.join('/', os.path.join(params.dir, params.file))
  File "/usr/lib/python2.6/posixpath.py", line 67, in join
    elif path == '' or path.endswith('/'):
AttributeError: 'NoneType' object has no attribute 'endswith'

I cannot past any code because is too long. What I am trying to understand is where the error lays or what part of the code is triggering the AttributeError. Can you please help me???

我无法通过任何代码,因为太长了。我想了解的是错误所在或代码的哪一部分触发了 AttributeError。你能帮我么???

采纳答案by zhangyangyu

The pathin the elifis a Noneand None == ''returns Falseso the remain will be executed. And backwards, the params.diris a None. You need to check your code where the params.dirgenerated to see how the Nonecome.

pathelifNoneNone == ''回报False,因此仍然会被执行。反过来,params.dir是一个None. 你需要检查你的代码在哪里params.dir生成,看看是怎么None来的。