sys.stdin.readlines()挂起Python脚本 [英] sys.stdin.readlines() hangs Python script

查看:158
本文介绍了sys.stdin.readlines()挂起Python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次执行Python脚本时,它似乎都挂在这行上:

Everytime I'm executing my Python script, it appears to hang on this line:

lines = sys.stdin.readlines()

我应该怎么做才能解决/避免这个问题?

What should I do to fix/avoid this?

编辑

这就是我在使用lines所做的事情:

Here's what I'm doing with lines:

lines = sys.stdin.readlines()
updates = [line.split() for line in lines]

编辑2

我正在从git钩子运行此脚本,所以EOF周围仍然存在吗?

I'm running this script from a git hook so is there anyway around the EOF?

推荐答案

这在很大程度上取决于您要完成的工作.您也许可以:

This depends a lot on what you are trying to accomplish. You might be able do:

for line in sys.stdin:
    #do something with line

当然,对于这个惯用语以及您正在使用的readlines()方法,您都需要以某种方式将EOF字符发送到脚本中,以便它知道可以读取文件了. (在Unix上,Ctrl-D通常可以解决问题).

Of course, with this idiom as well as the readlines() method you are using, you need to somehow send the EOF character to your script so that it knows that the file is ready to read. (On unix Ctrl-D usually does the trick).

这篇关于sys.stdin.readlines()挂起Python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆