我在字符串重定向和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)
这是因为你将参数作为一个列表传递。当您将参数作为iterable传递时,每个片段都被传递给派生的进程(在本例中,
gs
抱怨它不知道如何处理2>&1
…),如果您键入以下内容,您可能会得到相同的消息:在壳里。在
^{pr2}$会做你想做的或“更好的”。。。在
(更好,因为它避开了
shell=True
的安全问题)相关问题 更多 >
编程相关推荐