Python子进程模块Ghostscript 2>&1

2024-10-01 00:18:40 发布

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

我在字符串重定向和python的shell Ghostscript命令方面遇到了问题。在

有NP执行此操作:

subprocess.call(["gs", "-q","-dBATCH", "-dNOPAUSE","-sDEVICE=bbox", "input.pdf"])

但添加2>;1时出错:

^{pr2}$

或者:

subprocess.call(["gs", "-q","-dBATCH", "-dNOPAUSE","-sDEVICE=bbox", "input.pdf","2>&1",">/dev/null"])

我想用2>;1申请“grep”。在

错误示例:

Error: /undefinedfilename in (2>&1) Operand stack:

Execution stack: %interp_exit .runexec2 --nostringval--
--nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push Dictionary stack: --dict:1156/1684(ro)(G)--
--dict:1/20(G)-- --dict:77/200(L)-- Current allocation mode is local Last OS error: 2 GPL Ghostscript 9.05: Unrecoverable error, exit code 1 1

如何执行命令,如:

subprocess.call("gs -q -dBATCH -dNOPAUSE -sDEVICE=bbox input.pdf 2>&1
| egrep -v HiResBoundingBox | egrep -o "[0-9]{1,}",shell=True)

Tags: gtgsinputpdfstackshellcalldict
1条回答
网友
1楼 · 发布于 2024-10-01 00:18:40

这是因为你将参数作为一个列表传递。当您将参数作为iterable传递时,每个片段都被传递给派生的进程(在本例中,gs抱怨它不知道如何处理2>&1…),如果您键入以下内容,您可能会得到相同的消息:

gs -q -dBATCH -dNOPAUSE -sDEVICE=bbox input.pdf '2>&1' 

在壳里。在

^{pr2}$

会做你想做的或“更好的”。。。在

import sys
subprocess.call(["gs", "-q", "-dBATCH", "-dNOPAUSE", "-sDEVICE=bbox", "input.pdf"],stderr=sys.stdout)

(更好,因为它避开了shell=True的安全问题)

相关问题 更多 >