使用python运行yamllint命令行

我想使用python中的子进程模块运行yamllint(yaml_file)。

我知道还有另一个模块可以在python中调用yamllint,但是我想使用子进程来实现

 lint='yamllint("%s")'%(args.yaml)
 print(lint)

 p=subprocess.Popen(lint,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
 stdoutdata,stderr=p.communicate()
 if stderr:
            print(stderr)
            print("Please check YAML format")
            quit()

这给了我一个错误:b'/ bin / sh:-c:第0行:意外令牌"delete.yaml"\'\n/bin/sh: -c: line 0: yamllint(“ delete.yaml”)\'\ n'

附近的语法错误

请帮助我了解其中的错误

zxllpyj 回答:使用python运行yamllint命令行

从shell启动yamllint的语法是“ yamllint”-而不是“ yamllint()”。但是出于安全原因,您不应使用外壳来启动yaml。只需传递一个表达式数组即可:

lint=["yamllint",args.yaml]
p=subprocess.Popen(lint,shell=False,stdout=subprocess.PIPE,stderr=subprocess.PIPE)

但是请注意,您对sterr的测试不起作用。您也必须对此进行努力。

本文链接:https://www.f2er.com/3167322.html

大家都在问