【问题标题】:Stuck in while True loop python卡在 while True 循环 python
【发布时间】:2014-04-22 16:50:24
【问题描述】:

我陷入了一个我似乎无法打破的真正循环,请提供任何建议:

command1 = transporterLink + " -m verify -f " + indir1 + " -u " + username + " -p " + password + " -o " + indir1 + "/VerifyLog.txt -s " + provider1 + " -v eXtreme"
master, slave = pty.openpty()

process = Popen(command1, shell=True, stdin=PIPE, stdout=slave, stderr=slave, close_fds=True)
stdout = os.fdopen(master)
while True:
    wx.Yield()
    line = stdout.readline()
    print line.rstrip()
    if not line:
        break
process.wait()

【问题讨论】:

  • 你有没有从stdout得到一个空行?

标签: python while-loop


【解决方案1】:

最简单的解释是你永远不会从stdout 得到一个空行。注意print line.rstrip()不会修改line;例如,如果最后一行以换行符结束,则循环将继续。

【讨论】:

  • 感谢 NPE,有什么建议可以让我在最后摆脱这个循环吗?
  • @speedyrazor 你可以考虑if not line.strip():
【解决方案2】:

已排序。我知道在最后一行的末尾它将返回两个字符串之一,因此只需要搜索这两个字符串中的任何一个:

process = Popen(command1, shell=True, stdin=PIPE, stdout=slave, stderr=slave, close_fds=True)
stdout = os.fdopen(master)
while True:
    wx.Yield()
    line = stdout.readline()
    line = line.rstrip()
    print line
    if "Returning 1" in line:
        break
    if "Returning 0" in line:
        break

【讨论】:

    猜你喜欢
    • 2022-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-14
    相关资源
    最近更新 更多