Python执行系统命令

2019-10-24  本文已影响0人  imjcw

目前了解到的方式有 3 种。

os.system

# -*- coding: utf-8 -*-

import os

os.system('ps -ef | grep python | wc -l')

os.popen

文档地址:传送门

# -*- coding: utf-8 -*-

import os

os.popen('aa.txt', 'r', 1)

commands

可以执行命令,拿到执行结果,拿到执行状态,并且不直接输出,很棒

# -*- coding: utf-8 -*-

import commands

status, output = commands.getstatusoutput('ps -ef | grep php | wc -l')
output = commands.getoutput('ps -ef | grep php | wc -l')
上一篇 下一篇

猜你喜欢

热点阅读