尝试执行子进程调用时没有这样的文件或目录

时间:2017-02-21 23:36:49

标签: python

前言:我之前已经问过这个问题,但是从找到之前的答案后我找不到解决错误的方法。

我想要做的就是调用diff来输出同一文件中的两个不同命令。

 import os, sys
 from subprocess import check_call
 import shlex

 ourCompiler = 'espressoc';
 checkCompiler = 'espressocr';

 indir = 'Tests/Espresso/GoodTests';

 check_call(["pwd"]);

 for root, dirs, filenames in os.walk(indir):
     for f in filenames:
         if len(sys.argv) == 2 and sys.argv[1] == f:
             str1 = "<(./%s ./%s) " % (ourCompiler, os.path.join(root, f))
             str2 = "<(./%s ./%s) " % (checkCompiler, os.path.join(root, f))
             check_call(["diff", str1, str2])

为什么我收到以下错误?

diff: <(./espressoc ./Tests/Espresso/GoodTests/Init.java) : No such file or directory
diff: <(./espressocr ./Tests/Espresso/GoodTests/Init.java) : No such file or directory
Traceback (most recent call last):
  File "runTest.py", line 21, in <module>
    check_call(["diff", str1, str2])
  File "/usr/lib/python3.5/subprocess.py", line 581, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['diff', '<(./espressoc ./Tests/Espresso/GoodTests/Init.java) ', '<(./espressocr ./Tests/Espresso/GoodTests/Init.java) ']' returned non-zero exit status 2

如果我从我的shell运行此命令,它可以正常工作。

1 个答案:

答案 0 :(得分:0)

diff抱怨它无法找到名称为<(./espressoc ./Tests/Espresso/GoodTests/Init.java)的奇怪文件,因为这是你喂它的论据。

subprocess.Popencheck_call是一个便利功能)直接调用您提供的内容,不是shell 解释重定向或任何东西,除非你告诉它shell=True,然后它将通过/bin/sh(在POSIX上)调用命令。使用前请注意security considerations

基本上是这样的:

subprocess.check_call(['diff', '<this', '<that'])` # strange files.
subprocess.check_call('diff <this <that', shell=True)` # /bin/sh does some redirection

如果你想要“纯粹”(可能比它的价值更多的努力),我认为你可以对所有三个进程(diff,编译器1和2)进行子处理并自己处理管道。在关闭标准输入之前,diff是否等待2个EOF或其他东西?不确定它是如何实际处理双线输入重定向的,就像你的线有......

相关问题