Python论坛  - 讨论区

标题:[python-chinese] stdout 重定向为何无效?

2006年02月11日 星期六 18:02

魏忠 weizhong2004 at gmail.com
Sat Feb 11 18:02:37 HKT 2006

#test_stdout.py
sys.__stdout__=open(r'c:\log.txt','a')
print 'this is a book'
print 'that is a pen'
sys.__stdout__.close()

输出的内容还是放到了屏幕上

--

开飞机的舒克
http://www.lvye.org/shuke
msn:weizhong at netease.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060211/409f26bc/attachment.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月11日 星期六 18:20

Zoom Quiet zoom.quiet at gmail.com
Sat Feb 11 18:20:02 HKT 2006

On 2/11/06, 魏忠 <weizhong2004 at gmail.com> wrote:
> #test_stdout.py
> sys.__stdout__=open(r'c:\log.txt','a')
> print 'this is a book'
> print 'that is a pen'
> sys.__stdout__.close()
>
> 输出的内容还是放到了屏幕上
不如明显一些?
f = open( r'c:\log.txt', "a+")
print >> f,"this is a book"
print >> f,"that is a pen"
>
> --
>
> 开飞机的舒克
>  http://www.lvye.org/shuke
> msn:weizhong at netease.com
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request at lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>
>


--
"""Time is unimportant, only life important!
blogging  :  http://blog.zoomquiet.org/pyblosxom/
wiki enter:   http://wiki.woodpecker.org.cn/moin/ZoomQuiet
in douban:  http://www.douban.com/people/zoomq/
"""

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月11日 星期六 18:28

魏忠 weizhong2004 at gmail.com
Sat Feb 11 18:28:25 HKT 2006

象zoom兄那样做是可以的。
《Python In a Nutshell》(2003)
10.7.1 标准输出及标准错误
sys 模块有 stdout 和 stderr 属性, 这是用于输出的两个文件对象。 除非你使用**某种 shell
重定向**,输出内容将总是发送到执行脚本的终端上。当然现在几乎没有什么真正的终端了:这个所谓的终端通常是一个支持文本输入输出的窗口(比方windows下的一个
控制台 或unix下 一个 xterm 窗口).

我作了一个测试。发现

sys.stdout=...
sys.__stdout__=...
类似的赋值语句虽然执行无误,却并未真正改变 stdout 的值。或许与解释器的设置有关?




--

开飞机的舒克
http://www.lvye.org/shuke
msn:weizhong at netease.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060211/aeade385/attachment.htm

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月11日 星期六 18:31

sunqiang sunqiang at gmail.com
Sat Feb 11 18:31:06 HKT 2006

import sys
sys.stdout=open(r'c:\log.txt','a')
print 'this is a book'
print 'that is a pen'
sys.stdout.close()

On 2/11/06, Zoom Quiet <zoom.quiet at gmail.com> wrote:
>
> On 2/11/06, 魏忠 <weizhong2004 at gmail.com> wrote:
> > #test_stdout.py
> > sys.__stdout__=open(r'c:\log.txt','a')
> > print 'this is a book'
> > print 'that is a pen'
> > sys.__stdout__.close()
> >
> > 输出的内容还是放到了屏幕上
> 不如明显一些?
> f = open( r'c:\log.txt', "a+")
> print >> f,"this is a book"
> print >> f,"that is a pen"
> >
> > --
> >
> > 开飞机的舒克
> >  http://www.lvye.org/shuke
> > msn:weizhong at netease.com
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese at lists.python.cn
> > Subscribe: send subscribe to
> > python-chinese-request at lists.python.cn
> > Unsubscribe: send unsubscribe to
> > python-chinese-request at lists.python.cn
> > Detail Info:
> > http://python.cn/mailman/listinfo/python-chinese
> >
> >
>
>
> --
> """Time is unimportant, only life important!
> blogging  :  http://blog.zoomquiet.org/pyblosxom/
> wiki enter:   http://wiki.woodpecker.org.cn/moin/ZoomQuiet
> in douban:  http://www.douban.com/people/zoomq/
> """
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060211/fc549b33/attachment.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月11日 星期六 19:15

魏忠 weizhong2004 at gmail.com
Sat Feb 11 19:15:34 HKT 2006

楼上老兄测试了么?
这个代码据俺测试也是不行的

开飞机的舒克
http://www.lvye.org/shuke
msn:weizhong at netease.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060211/b393c4b4/attachment.htm

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月11日 星期六 19:21

Andelf andelf at gmail.com
Sat Feb 11 19:21:35 HKT 2006

根据我的测试
open 改成file就可以了
原因不明


在06-2-11,魏忠 <weizhong2004 at gmail.com> 写道:
>
> 楼上老兄测试了么?
> 这个代码据俺测试也是不行的
>
> 开飞机的舒克
> http://www.lvye.org/shuke
> msn:weizhong at netease.com
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>


--
Andelf
BLOG:http://blog.sohu.com/members/andelf/
BLOG:http://spaces.msn.com/members/andelf
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060211/2b266b91/attachment.htm

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月11日 星期六 19:39

Andelf andelf at gmail.com
Sat Feb 11 19:39:01 HKT 2006

呃,再测试
似乎都可以
魏大叔,你用的什么平台?


在06-2-11,Andelf <andelf at gmail.com> 写道:
>
> 根据我的测试
> open 改成file就可以了
> 原因不明
>
>
> 在06-2-11,魏忠 <weizhong2004 at gmail.com> 写道:
> >
> > 楼上老兄测试了么?
> > 这个代码据俺测试也是不行的
> >
> > 开飞机的舒克
> > http://www.lvye.org/shuke
> > msn:weizhong at netease.com
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese at lists.python.cn
> > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
> >
>
>
> --
> Andelf
> BLOG:http://blog.sohu.com/members/andelf/
> BLOG: http://spaces.msn.com/members/andelf




--
Andelf
BLOG:http://blog.sohu.com/members/andelf/
BLOG:http://spaces.msn.com/members/andelf
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060211/1e856998/attachment.htm

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月11日 星期六 19:40

sunqiang sunqiang at gmail.com
Sat Feb 11 19:40:23 HKT 2006

嗯,我的是file,不知道为啥copy出来成open了...

On 2/11/06, 魏忠 <weizhong2004 at gmail.com> wrote:
>
> 楼上老兄测试了么?
> 这个代码据俺测试也是不行的
>
> 开飞机的舒克
> http://www.lvye.org/shuke
> msn:weizhong at netease.com
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060211/27749938/attachment.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月11日 星期六 19:42

Andelf andelf at gmail.com
Sat Feb 11 19:42:55 HKT 2006

那file和open有什么区别啊?



在06-2-11,sunqiang <sunqiang at gmail.com> 写道:
>
> 嗯,我的是file,不知道为啥copy出来成open了...
>
>


--
Andelf
BLOG:http://blog.sohu.com/members/andelf/
BLOG:http://spaces.msn.com/members/andelf
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060211/a131092a/attachment.htm

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月11日 星期六 19:43

sunqiang sunqiang at gmail.com
Sat Feb 11 19:43:57 HKT 2006

hoho,开了两个窗口,所以copy错了,不过好像open也可以的。

On 2/11/06, Andelf <andelf at gmail.com> wrote:
>
> 呃,再测试
> 似乎都可以
> 魏大叔,你用的什么平台?
>
>
> 在06-2-11,Andelf <andelf at gmail.com> 写道:
> >
> > 根据我的测试
> > open 改成file就可以了
> > 原因不明
> >
> >
> > 在06-2-11,魏忠 <weizhong2004 at gmail.com> 写道:
> > >
> > > 楼上老兄测试了么?
> > > 这个代码据俺测试也是不行的
> > >
> > > 开飞机的舒克
> > > http://www.lvye.org/shuke
> > > msn:weizhong at netease.com
> > >
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese at lists.python.cn
> > > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > > Unsubscribe: send unsubscribe to
> > > python-chinese-request at lists.python.cn
> > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> > >
> > >
> >
> >
> > --
> > Andelf
> > BLOG:http://blog.sohu.com/members/andelf/
> > BLOG: http://spaces.msn.com/members/andelf
>
>
>
>
> --
> Andelf
> BLOG:http://blog.sohu.com/members/andelf/
> BLOG: http://spaces.msn.com/members/andelf
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060211/845dd46a/attachment.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月11日 星期六 19:44

sunqiang sunqiang at gmail.com
Sat Feb 11 19:44:43 HKT 2006

先有open,然后有file,好像现在是推荐大家用file...

On 2/11/06, Andelf <andelf at gmail.com> wrote:
>
> 那file和open有什么区别啊?
>
>
>
> 在06-2-11,sunqiang <sunqiang at gmail.com> 写道:
> >
> > 嗯,我的是file,不知道为啥copy出来成open了...
> >
> >
>
>
> --
> Andelf
> BLOG: http://blog.sohu.com/members/andelf/
> BLOG:http://spaces.msn.com/members/andelf
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060211/9a3cd217/attachment.htm

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月11日 星期六 19:57

魏忠 weizhong2004 at gmail.com
Sat Feb 11 19:57:37 HKT 2006

问题解决,是俺自己没看清楚,嘿嘿
当解释器刚刚启动时,使用 sys.__stdout__,当解释器启动完成后,使用 sys.stdout
刚才换用 sys.stdout 之后其实已经生效了,俺粗心未去检查 log.txt 的内容。
file与open是别名的关系,可以用下面的方式验证:
>>> file

>>> open

>>> file==open
True
>>> file is open
True
>>>

C:\>edit test.py

C:\>type test.py
import sys
sys.stdout=open('log.txt','a')
print sys.stdout
print 'this is a book'
print 'that is a pen'
sys.__stdout__.close()

C:\>python test.py

C:\>type log.txt

this is a book
that is a pen

this is a book
that is a pen
--

开飞机的舒克
http://www.lvye.org/shuke
msn:weizhong at netease.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060211/753e988b/attachment.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号