#!/usr/bin/python
import subprocess
f = open("output.txt", "w")
print>>f, "/etc/hosts output:n"
print>>f, "____________________n"
subprocess.call('cat /etc/hosts',stdout=f,shell=True)
print>>f, "hostname output:n"
print>>f, "____________________"
subprocess.call('hostname',stdout=f,shell=True)
下面是输出.txt文件。
127.0.0.1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
127.0.0.1 localhost localhost.localdomain
::1 localhost localhost.localdomain
node1.example.com
/etc/hosts output:
hostname output:
我们需要这个输出.txt
格式如下。/etc/hosts output:
____________________
127.0.0.1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
127.0.0.1 localhost localhost.localdomain
::1 localhost localhost.localdomain
hostname output:
____________________
node1.example.com
子进程将与应用程序异步工作,这就是为什么这种行为,但是您也可以这样做:
#!/usr/bin/python
import subprocess
f = open("output.txt", "w")
#print>>f, "/etc/hosts output:n"
#print>>f, "____________________n"
subprocess.call('echo "/etc/hosts output:" n echo "____________________" n cat /etc/hosts',stdout=f,shell=True)
#print>>f, "hostname output:n"
#print>>f, "____________________"
subprocess.call('echo "hostname output:" n echo "____________________" n hostname',stdout=f,shell=True)