使用--exclude运行diff命令在python中不起作用

时间:2018-03-06 05:20:44

标签: python diff

我在MAC上运行diff命令如下,它运行正常但是当我运行python时--exclude选项不起作用意味着命令输出仍然列出{{1}下的文件},任何人都可以建议如何调试此或如何解决此问题

/Users/username/FWIntegration/branch_4355c1/.git

从python

运行
/usr/bin/diff -x '.*' -x 'tech' -rq /Users/username/FWIntegration/repo2mirror /Users/username/FWIntegration/branch_4355c1 --exclude=/Users/username/FWIntegration/branch_4355c1/.git/

它列出了

cmd = "/usr/bin/diff -x '.*' -x 'tech' -rq /Users/username/FWIntegration/repo2mirror /Users/username/FWIntegration/branch_4355c1 --exclude=/Users/username/FWIntegration/branch_4355c1/.git/"

output,error = runCmd(cmd)

def runCmd(cmd):
    out = ""
    err = ""
    logger.info("Running command %s"%cmd)
    proc = Popen(cmd.split(' '), stdout=PIPE, stderr=PIPE)
    try:
        with proc.stdout as stdout:
            for line in stdout:
                print line,
                out = out + line
                #android_loader_output+=line
                #if 'ERROR:' in line:
                    #print line
    except:
        print "%s failed"%cmd
        print traceback.format_exc()

    try:
        with proc.stderr as stderr:
            for line in stderr:
                print line,
                err = err + line
                #android_loader_output+=line
                #if 'ERROR:' in line:
                    #print line
    except:
        print "%s failed"%cmd
        print traceback.format_exc()

    #print out
    #print err
    return out,err

1 个答案:

答案 0 :(得分:1)

问题是由cmd字符串中的引号引起的。您没有使用shell处理命令,而是使用cmd.split()来解析它,因此这些引号将逐字地发送给程序。

使用shell以便正确解析所有内容:

proc = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)