使用python运行控制台命令

2024-09-29 01:29:41 发布

您现在位置:Python中文网/ 问答频道 /正文

此代码直接在CLI中工作正常:

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

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

^{pr2}$

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

OSError: [Errno 2] No such file or directory

Tags: 代码test命令appcli环境xmlxpath
1条回答
网友
1楼 · 发布于 2024-09-29 01:29:41

如果与派生进程无关,则可以使用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将程序名及其参数作为字符串列表,这可能是问题所在

相关问题 更多 >