Python doctest example failure
This is probably a silly question.
I am experimenting with python doctest, and I try to run this example
ending with
if __name__ == "__main__":
import doctest
doctest.testfile("e开发者_开发知识库xample.txt")
I have put "example.txt" in the same folder as the source file containing the example code, but I get the following error:
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'
Can I somehow tell/set where the doctest module is searching for the specified file?
Doctest searches relative to the calling module's directory by default (but you can override this).
Quoting the docs for doctest.testfile
:
Optional argument
module_relative
specifies how the filename should be interpreted:
- If
module_relative
is True (the default), thenfilename
specifies an OS-independent module-relative path. By default, this path is relative to the calling module’s directory; but if thepackage
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, thenfilename
specifies an OS-specific path. The path may be absolute or relative; relative paths are resolved with respect to the current working directory.
精彩评论