加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python subprocess.check_call – 如何将stdout和stderr指向_bot

发布时间:2020-12-20 13:28:38 所属栏目:Python 来源:网络整理
导读:我使用 Python 2.7.这就是我目前正在做的从Python启动其他程序,将STDOUT和STERR重定向到LOGFILE: try: # Execute command and print content to LOGFILE tempname = os.path.abspath('./text.txt') TEMPFILE = open(tempname,'wb') print 'Executing: ',com
我使用 Python 2.7.这就是我目前正在做的从Python启动其他程序,将STDOUT和STERR重定向到LOGFILE:

try:
    # Execute command and print content to LOGFILE
        tempname = os.path.abspath('./text.txt')
        TEMPFILE = open(tempname,'wb')
        print 'Executing: ',command
        subprocess.check_call(command,shell = True,stdout = TEMPFILE,stderr = TEMPFILE)
        TEMPFILE.close()
        LOGFILE.write(open(tempname,'rU').read())
        LOGFILE.close()
        os.remove(tempname)
    except Exception as errmsg:
    # If fails then print errors to LOGFILE
        TEMPFILE.close()
        LOGFILE.write(open(tempname,'rU').read())
        os.remove(tempname)
        print messages.crit_error_bad_command % command,'n',str(errmsg)
        print >> LOGFILE,messages.crit_error_bad_command % command,str(errmsg)
        LOGFILE.close()

首先,我不确定我的上述脚本是最好的解决方案.我不得不使用临时文件,因为即使在subprocess.check_call调用失败的情况下我也想捕获日志.如果您有如何改进上述脚本的想法,我会很感激.

此外,我想更改此设置,以便STDOUT和STDERR应该正常进入屏幕,也可以进入日志文件.我该怎么做呢?请注意,如果我没有指定STDOUT,我会看到“有错误,你想继续[y / n]吗?”在屏幕上然后我可以对它作出反应.现在,因为一切都进入日志,我在屏幕上看不到任何内容.这里我的问题的答案应该有助于解决这个问题.

谢谢.

解决方法

I’ll see things like “There is an error,do you wish to continue [y/n]?” on the screen and then I can react to it.

要重定向子进程’stdout并手动或使用预定义的答案与进程交互,您可以使用pexpect:

from contextlib import closing
import pexpect

with open('text.txt','rb') as logfile:
    with closing(pexpect.spawn(command,logfile=logfile)) as child:
        # call here child.expect(),child.sendline(),child.interact(),etc

使用subprocess.Popen实现相同的功能是not trivial.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读