python调用git bash_运行Python代码时,Cmd和Git bash有不同的结果

所以我看了一下这个并尝试了几种不同的方式来编写你所拥有的东西,他们都采取了同样的方式.我进入了更多,我遇到了

https://code.google.com/p/mintty/issues/detail?id=218.关键在于andy.koppe的回复:

The key to the problem is that stdout’s default buffering mode depends on the type of device: unbuffered for a console, buffered for a pipe. This means that in a console the output will appear immediately, whereas in mintty it will only appear once the buffer is either full or flushed, as happens at the end of main().

Windows控制台尽快将文本打印到屏幕上,而mingw(git bash)将等到应用程序告诉它更新屏幕.

因此,要使它在两者中表现相同,您需要在每次打印后将缓冲区刷新到屏幕. How to flush output of Python print?有关于如何执行此操作的信息,但它归结为以下内容:

import sys

print "How old are you?"

sys.stdout.flush()

age = raw_input()

print "How tall are you?"

sys.stdout.flush()

height = raw_input()

print "How much do you weigh?"

sys.stdout.flush()

weight = raw_input()

print "So, you're %r old, %r tall and %r heavy." % (age, height, weight)

或者你可以使用-u命令在mingw中运行它,这将阻止python缓冲mingw中的输出.

python -u file.py

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值