V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
SlipStupig
V2EX  ›  Python

Python doctest 结果死活写不到文件里面去

  •  
  •   SlipStupig · 2017-03-22 10:12:51 +08:00 · 1577 次点击
    这是一个创建于 2602 天前的主题,其中的信息可能已经有所发展或是发生改变。

    系统:Ubuntu
    python:2.7.6
    我看别人都能写到文件里面, 我只能输出到终端中,难道我的打开方式有问题
    这个是我看的官方实例,也不行

    
    def factorial(n):
        """Return the factorial of n, an exact integer >= 0.
    
        If the result is small enough to fit in an int, return an int.
        Else return a long.
    
       >>> # comments are ignored
        >>> # comments are ignored
        >>> x = 12
        >>> x
        
        >>> if x == 13:
        ...     print "yes"
        ... else:
        ...     print "no"
        ...     print "NO"
        ...     print "NO!!!"
        ...
    
    
        """
    
        import math
        if not n >= 0:
            raise ValueError("n must be >= 0")
        if math.floor(n) != n:
            raise ValueError("n must be exact integer")
        if n+1 == n:  # catch a value like 1e300
            raise OverflowError("n too large")
        result = 1
        factor = 2
        while factor <= n:
            result *= factor
            factor += 1
        return result
    
    
    if __name__ == "__main__":
        import doctest
        doctest.testmod()
    
    
    4 条回复    2017-03-22 14:51:59 +08:00
    jason0916
        1
    jason0916  
       2017-03-22 10:27:18 +08:00
    我这边试了一下也是不行啊,会不会是搞错了?

    我看文档里面的例子的第二部分是执行文件里的测试用例而不是输出结果到文件里,两个部分都试验了一下结果都是输出在终端的
    SlipStupig
        2
    SlipStupig  
    OP
       2017-03-22 11:26:16 +08:00
    jason0916
        3
    jason0916  
       2017-03-22 13:37:18 +08:00
    @SlipStupig 文档我有看,你要不试试在命令行加上“> test.log ” 把标准输出定向到文件?
    SlipStupig
        4
    SlipStupig  
    OP
       2017-03-22 14:51:59 +08:00
    @jason0916 我关键是想把结果输出到代码里面,方便输出文档
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   965 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 21:11 · PVG 05:11 · LAX 14:11 · JFK 17:11
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.