Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | 51CTO学院 | CSDN程序员研修院 | OSChina 博客 | 腾讯云社区 | 阿里云栖社区 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏多维度架构

2.8. subprocess

2.8.1. check_output

			
sp = subprocess.check_output(cmd)
text = sp.decode('UTF8')
print(text)
		
		

获取IP地址

		 
import subprocess

command = "/usr/bin/ip addr show eth0 | grep 'inet ' | awk '{print $2}' | cut -d/ -f1"
screen = subprocess.check_output(command, shell=True)
print(screen.decode().replace("\n", ""))		
		
		

制定运行目录

		 
#!/usr/bin/python
# -*-coding:utf-8-*-
import subprocess

output = subprocess.check_output("ls", cwd="/")
print(output.decode())

output = subprocess.check_output("/usr/bin/git pull", cwd="/opt/netkiller", shell=True)
print(output.decode())