[英]How to redirect stdout stderr of os.execlpe() into a file
I am hitting my head against the wall, running a jpyter nbconvert
from python the following way, as it allows me to pass arguments to the jupyter notebook: 我撞到了墙上,按照以下方式从python运行
jpyter nbconvert
,因为它允许我将参数传递给jupyter笔记本:
env['IPYTHONARGV'] = json.dumps({'timeperiod':timeperiod,'infile':infile})
os.execlpe('jupyter', 'jupyter', 'nbconvert', '--execute','notebook.ipynb',
'--to', 'html', '--output', output_html, '2>&1', '1>log.out', env)
When leaving out the '2>&1', '1>log.out',
part, the command works just fine. 当省略
'2>&1', '1>log.out',
部分时,该命令可以正常工作。 But with the bash redirect, the command complains the following: 但是使用bash重定向时,该命令将抱怨以下内容:
[NbConvertApp] WARNING | pattern '2>&1' matched no files
[NbConvertApp] WARNING | pattern '1>log.out' matched no files
Does anybody know how to solve that problem? 有人知道如何解决这个问题吗?
The redirections 2>&1
ad 1>log.out
are interpreted by the shell, but you are providing them to the command as arguments. 外壳
1>log.out
会解释重定向2>&1
ad 1>log.out
,但是您1>log.out
它们作为参数提供给命令。 That's why Jupyter complains about not being able to find them as files. 这就是Jupyter抱怨无法将它们作为文件找到的原因。
You can use subprocess
with shell=True
: 您可以将
subprocess
与shell=True
:
import subprocess as sp
env['IPYTHONARGV'] = json.dumps({'timeperiod':timeperiod,'infile':infile})
sp.check_call('jupyter nbconvert --execute notebook.ipynb --to html --output output_html 2>&1 1>log.out', shell=True, env=env)
You can use sp.check_output()
and remove the redirect if you need to process the output in Python. 如果需要在Python中处理输出,则可以使用
sp.check_output()
并删除重定向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.