gpt4 book ai didi

Python doctest 失败示例

转载 作者:行者123 更新时间:2023-11-30 23:57:09 27 4
gpt4 key购买 nike

这可能是一个愚蠢的问题。

我正在尝试使用 python doctest,并且尝试运行 this示例

结尾为

if __name__ == "__main__":
import doctest
doctest.testfile("example.txt")

我已将“example.txt”放在与包含示例代码的源文件相同的文件夹中,但出现以下错误:

Traceback (most recent call last):
File "test_av_funktioner.py", line 61, in <module>
doctest.testfile("example.txt")
File "C:\Python26\lib\doctest.py", line 1947, in testfile
text, filename = _load_testfile(filename, package, module_relative)
File "C:\Python26\lib\doctest.py", line 219, in _load_testfile
return open(filename).read(), filename
IOError: [Errno 2] No such file or directory: 'example.txt'

我可以以某种方式告诉/设置 doctest 模块在哪里搜索指定文件吗?

最佳答案

默认情况下,Doctest 相对于调用模块的目录进行搜索(但您可以覆盖它)。

引用 doctest.testfile 的文档:

Optional argument module_relative specifies how the filename should be interpreted:

  • If module_relative is True (the default), then filename specifies an OS-independent module-relative path. By default, this path is relative to the calling module’s directory; but if the package argument is specified, then it is relative to that package. To ensure OS-independence, filename should use / characters to separate path segments, and may not be an absolute path (i.e., it may not begin with /).
  • If module_relative is False, then filename specifies an OS-specific path. The path may be absolute or relative; relative paths are resolved with respect to the current working directory.

关于Python doctest 失败示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3853980/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com