python交互式编程输错时,在使用Python管道编写脚本时无法启动交互式程序

I have a python script that needs to call the defined $EDITOR or $VISUAL. When the Python script is called alone, I am able to launch the $EDITOR without a hitch, but the moment I pipe something to the Python script, the $EDITOR is unable to launch. Right now, I am using nano which shows

Received SIGHUP or SIGTERM

every time. It appears to be the same issue described here.

sinister:Programming [1313]$ echo "import os;os.system('nano')" > "sample.py"

sinister:Programming [1314]$ python sample.py

# nano is successfully launched here.

sinister:Programming [1315]$ echo "It dies here." | python sample.py

Received SIGHUP or SIGTERM

Buffer written to nano.save.1

EDIT: Clarification; inside the program, I am not piping to the editor. The code is as follows:

editorprocess = subprocess.Popen([editor or "vi", temppath])

editorreturncode = os.waitpid(editorprocess.pid, 0)[1]

解决方案

When you pipe something to a process, the pipe is connected to that process's standard input. This means your terminal input won't be connected to the editor. Most editors also check whether their standard input is a terminal (isatty), which a pipe isn't; and if it isn't a terminal, they'll refuse to start. In the case of nano, this appears to cause it to exit with the message you included:

% echo | nano

Received SIGHUP or SIGTERM

You'll need to provide the input to your Python script in another way, such as via a file, if you want to be able to pass its standard input to a terminal-based editor.

Now you've clarified your question, that you don't want the Python process's stdin attached to the editor, you can modify your code as follows:

editorprocess = subprocess.Popen([editor or "vi", temppath],

stdin=open('/dev/tty', 'r'))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值