如何重定向的stdout stderr操作系统执行()变成fi

2024-04-25 12:24:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我的头撞在墙上,用下面的方式运行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)

当省略'2>&1', '1>log.out',部分时,该命令工作正常。但是对于bash重定向,命令会抱怨如下:

[NbConvertApp] WARNING | pattern '2>&1' matched no files
[NbConvertApp] WARNING | pattern '1>log.out' matched no files

有人知道怎么解决那个问题吗?你知道吗


Tags: no命令envlogoutputhtmljupyterout
1条回答
网友
1楼 · 发布于 2024-04-25 12:24:20

重定向2>&1ad 1>log.out由shell解释,但您将它们作为参数提供给命令。这就是为什么Jupyter抱怨找不到文件的原因。你知道吗

可以将subprocessshell=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)

如果需要用Python处理输出,可以使用sp.check_output()并删除重定向。你知道吗

相关问题 更多 >