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

问题描述 投票:0回答:1

我在MAC上运行diff命令如下,它工作正常但是当我运行python时,--exclude选项不起作用意味着命令输出仍然列出/Users/username/FWIntegration/branch_4355c1/.git下的文件,任何人都可以建议如何调试这个或如何解决这个问题

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

从python运行

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

它列出了像

Only in /Users/username/FWIntegration/branch_4355c1/.git/refs/tags: DIN2944T146R6_REL_9_74_5 
Only in /Users/username/FWIntegration/branch_4355c1/.git/refs/tags: DIN2944T18R2_REL_9_48_1 
Only in /Users/username/FWIntegration/branch_4355c1/.git/refs/tags: DIN2944T51R2_REL_9_55_2
python diff
1个回答
1
投票

问题是由于你在cmd字符串中的引号。你没有使用shell处理命令,你正在使用cmd.split()来解析它,所以这些引号正在逐字地发送给程序。

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

proc = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
© www.soinside.com 2019 - 2024. All rights reserved.