[英]Output of os.system in Python and use it to create a text node in a xml file
我想使用python os.system
函數執行os.system
docker ps -q
命令,然后獲取其輸出以使用它創建xml文本節點。
我嘗試了xml.createTextNode(os.system("docker ps -q")
:
6 from xml.dom import minidom
7 import os
8
9 xml = minidom.Document()
10
11 rootElem = xml.createElement('containers')
12
13 dataElem = xml.createElement('data')
14
15 idElem = xml.createElement('id')
16 idElem.appendChild(xml.createTextNode(os.system("docker ps -q")))
但這給了我這個錯誤:
File "scriptCreateXML.py", line 16, in <module>
idElem.appendChild(xml.createTextNode(os.system("docker ps -q")))
File "/usr/lib/python3.6/xml/dom/minidom.py", line 1658, in createTextNode
raise TypeError("node contents must be a string")
TypeError: node contents must be a string
我希望這個輸出
<?xml version="1.0" ?>
<containers>
<data>
<id>some id</id>
</data>
</containers>
修改最后一行,使用subprocess
check_output
代替os
並使用check_output
代替call
In [25]: idElem.appendChild(xml.createTextNode(subprocess.check_output(["docker","ps", "-aq"]).decode('UTF-8')))
Out[25]: <DOM Text node "'967dd77436'...">
我沒有運行中的容器,因此我使用了“ -aq”,您可以根據需要進行修改。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.