【问题标题】:Run console command with python使用 python 运行控制台命令
【发布时间】:2016-02-07 15:02:37
【问题描述】:

此代码直接在 CLI 中运行良好:

xmllint --xpath '//Entities[contains(ExpirationDate, '2015')]' app/sftp/file.xml > test.xml

现在,我需要在 Python 环境中执行相同的命令。这就是我正在尝试的:

def process_xml(path):
    call(["xmllint --xpath '//Entities[contains(ExpirationDate, '2015')]' app/sftp/file.xml > test.xml"])

这是错误,我正在同一位置运行文件和命令:

OSError: [Errno 2] No such file or directory

【问题讨论】:

标签: python linux python-2.7 xmllint


【解决方案1】:

如果您与生成的进程无关,则可以使用 os.system()

但是,如果你真的想使用subprocess.call 和流重定向,你必须像下面这样使用它:

with open('myfile', 'w') as outfile:
    subprocess.call(["xmllint", "--xpath", "//Entities[contains(ExpirationDate, '2015')]", "app/sftp/file.xml"], stdout=outfile)

请注意subprocess.call 将程序名称及其参数作为字符串列表,可能是问题所在

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-12
    • 2014-12-17
    • 1970-01-01
    • 1970-01-01
    • 2019-03-25
    • 2016-10-08
    • 2011-02-12
    • 1970-01-01
    相关资源
    最近更新 更多