首页 > 解决方案 > 尝试使用追加到文件引发 AttributeError 异常

问题描述

我正在尝试将存储在变量中的数据附加到文件中。文件创建成功,数据存储在变量中;但是,我继续收到 AttiributeError:

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

这是我的结果

KEYWORD ${FileA} = OperatingSystem . Create File metrics/Live_RTT.txt, ${MT_Com_RTT_LB_Live_Topics}
00:00:05.233 KEYWORD Selenium2Library . Click Element ${BLANK_CANVAS}
00:00:05.129 KEYWORD Selenium2Library . Click Element //div[@title='${device1}']
00:00:05.063 KEYWORD Selenium2Library . Click Element //div[@title='${device2}']
00:00:00.045 KEYWORD ${MT_Com_RTT_LB_Live_Topics} = Selenium2Library . Get Text xpath=${REAL_TIME_TRENDING_TOPICS}
00:00:00.001 KEYWORD BuiltIn . Log ${MT_Com_RTT_LB_Live_Topics}
Documentation:  

Logs the given message with the given level.
Start / End / Elapsed:  20181028 21:37:34.971 / 20181028 21:37:34.972 / 00:00:00.001
21:37:34.971    INFO    Available Topics
Active Energy Delivered + Received
Active Energy Into the Load
Active Energy Out of the Load
Active Power
Active Power A  
00:00:00.001 KEYWORD OperatingSystem . Append To File ${FileA}, ${MT_Com_RTT_LB_Live_Topics}
Documentation:  

Appends the given content to the specified file.
Start / End / Elapsed:  20181028 21:37:34.972 / 20181028 21:37:34.973 / 00:00:00.001
21:37:34.973    FAIL    AttributeError: 'NoneType' object has no attribute 'replace'

标签: robotframework

解决方案


查看库的源代码,最有可能在方法中引发异常,该normalize_path()方法执行此字符串替换:

    path = os.path.normpath(os.path.expanduser(path.replace('/', os.sep)))

,文件名在哪里- kw 调用path的第一个参数。Append To File异常说它的值是None(例如没有设置值),而它应该是一些字符串。

查看日志,您将此值设置为的返回值Create File- 但该关键字根本不返回一个,它只是创建您在其第一个参数中指定的文件。
所以要解决 - 只需设置${FileA}自己的值,将其传递给两个关键字,并且不要在Create File调用中重新分配。


推荐阅读