python 怎么shell 调用另一个脚本shell

python调用shell并读取返回信息_乐智雄吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0可签7级以上的吧50个
本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:2贴子:
python调用shell并读取返回信息
os.popen(command,mode)
打开一个与command进程之间的管道。这个函数的返回值是一个文件对象,可以读或者写(由mode决定,mode默认是’r')。如果mode为’r',可以使用此函数的返回值调用read()来获取command命令的执行结果。1.3
commands.getstatusoutput(command)  使用os.popen()函数执行command命令并返回一个元组(status,output),分别表示command命令执行的返回状态和执行结果。对command的执行实际上是按照{} 2&&1的方式,所以output中包含控制台输出信息或者错误信息。output中不包含尾部的换行符。 2.1 subprocess.call([&some_command&,&some_argument&,&another_argument_or_path&])
subprocess.call(command,shell=True)2.2
subprocess.Popen(command, shell=True)
如果command不是一个可执行文件,shell=True不可省。
使用subprocess模块可以创建新的进程,可以与新建进程的输入/输出/错误管道连通,并可以获得新建进程执行的返回状态。使用subprocess模块的目的是替代os.system()、os.popen*()、commands.*等旧的函数或模块。  最简单的方法是使用class subprocess.Popen(command,shell=True)。Popen类有Popen.stdin,Popen.stdout,Popen.stderr三个有用的属性,可以实现与子进程的通信。 将调用shell的结果赋值给python变量handle = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)municate()[0]
3D双端东方魔幻网游「大青云」勾魂公测,穿越逆转,封神故事,全新演绎!
打算用python程序调用shell命令并且获得返回信息,再用正则表达式提取信息类似爬虫,更进一步控制树莓派GPIO端口,控制灯的亮灭来反映实时网速
pychrame加中文注释好像有点问题,不过代码比较短,索性不加了python代码如下:import subprocessimport osimport timeimport reping = subprocess.Popen('ping ', shell=True, stdout=subprocess.PIPE)info = municate()[0]print infof = file(&info.txt&, &w&)f.writelines(info)f.close()f = open('info.txt', 'r')html = f.read()f.close()time = re.findall('\xf9 = (.*?)ms', html, re.S)print time
[172.18.1.3] ???? 32 ????????:???? 172.18.1.3 ????: ???=32 ???=5ms TTL=63???? 172.18.1.3 ????: ???=32 ???=7ms TTL=63???? 172.18.1.3 ????: ???=32 ???=9ms TTL=63???? 172.18.1.3 ????: ???=32 ???=3ms TTL=63172.18.1.3 ?? Ping ??????:
?????: ????? = 4??????? = 4????? = 0 (0% ???)???????г????????(????????λ):
??? = 3ms??? = 9ms????? = 6msinfo.txt里的信息如上,只抓取最后一个平均时间6打字好慢,该练练了。。。。
贴吧热议榜
使用签名档&&
保存至快速回贴Python执行shell命令四法_Linux编程_Linux公社-Linux系统门户网站
你好,游客
Python执行shell命令四法
来源:Linux社区&
作者:koumm
整理:Python执行shell命令四法,示例如下:
#!/usr/bin/env python&
# -*- coding: utf-8 -*-
import platform
import subprocess
import commands
def subproc():
& & print "系统进程数:"
& & subprocess.call("ps -ef|wc -l",shell=True)
def os_popen():
& & print "IP地址:"
& & os1 = platform.system()
& & if os1 == "Linux":
ip1 =os.popen("/sbin/ifconfig eth0|grep 'inet addr'").read().strip().split(":")[1].split()[0]
print "\033[1;32;40m%s\033[0m" % ip1
def os_system():
& & os_command = 'free -m'
& & cls_node1 = "命令执行成功...."
& & cls_node2 = "命令执行失败...."
& & if os.system(os_command) == 0:
& & & & print "\n\033[1;32;40m%s\033[0m" % cls_node1
& & & & print "\n\033[1;31;40m%s\033[0m" % cls_node2
def os_commands():
& & (status, output) = commands.getstatusoutput('pwd')
& & print status, output
def main():
& & subproc()
& & os_popen()
& & os_system()
& & os_commands()
if __name__ == "__main__":
& & main()
Python调用shell命令&
《Python核心编程 第二版》.(Wesley J. Chun ).[高清PDF中文版]
《Python开发技术详解》.( 周伟,宗杰).[高清PDF扫描版+随书视频+代码]
Python脚本获取Linux系统信息
在下用Python搭建桌面算法交易研究环境
Python 的详细介绍:Python 的下载地址:
本文永久更新链接地址:
相关资讯 & & &
   同意评论声明
   发表
尊重网上道德,遵守中华人民共和国的各项有关法律法规
承担一切因您的行为而直接或间接导致的民事或刑事法律责任
本站管理人员有权保留或删除其管辖留言中的任意内容
本站有权在网站内转载或引用您的评论
参与本评论即表明您已经阅读并接受上述条款python中执行shell命令的几个方法小结
投稿:junjie
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了python中执行shell命令的几个方法,本文一共给出3种方法实现执行shell命令,需要的朋友可以参考下
最近有个需求就是页面上执行shell命令,第一想到的就是os.system,
os.system('cat /proc/cpuinfo')
但是发现页面上打印的命令执行结果 0或者1,当然不满足需求了。
尝试第二种方案 os.popen()
output = os.popen('cat /proc/cpuinfo')
print output.read()
通过 os.popen() 返回的是 file read 的对象,对其进行读取 read() 的操作可以看到执行的输出。但是无法读取程序执行的返回值)
尝试第三种方案 commands.getstatusoutput() 一个方法就可以获得到返回值和输出,非常好用。
(status, output) = commands.getstatusoutput('cat /proc/cpuinfo')
print status, output
Python Document 中给的一个例子,
&&& import commands
&&& commands.getstatusoutput('ls /bin/ls')
(0, '/bin/ls')
&&& commands.getstatusoutput('cat /bin/junk')
(256, 'cat: /bin/junk: No such file or directory')
&&& commands.getstatusoutput('/bin/junk')
(256, 'sh: /bin/junk: not found')
&&& commands.getoutput('ls /bin/ls')
&&& commands.getstatus('/bin/ls')
'-rwxr-xr-x 1 root 13352 Oct 14 1994 /bin/ls'
最后页面上还可以根据返回值来显示命令执行结果。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具使用 Python 创建你自己的 Shell(下)_Linux中国_传送门
你是真实用户吗(Are you a robot)?
我们怀疑你不是真实用户,已对你的访问做了限制。如果您是真实用户,非常抱歉我们的误判对您造成的影响,您可以通过QQ()或电子邮件()反馈给我们,并在邮件和QQ请求信息里注明您的IP地址:220.177.198.53,我们会尽快恢复您的正常访问权限。另外,如果您不是在访问的当前页面,我们建议您移步
或者 在浏览器中输入以下地址:http://chuansong.me/n/ 访问,您所访问的网站是从抓取的数据,请直接访问,会有更好的体验和更及时的更新。We suspect you are a robot.We are really sorry if you are not,and you can email us () with your current IP address: 220.177.198.53 to get full access to .If you are not accessing
for the current page,you'd better visit
for better performance,as the current website you are accessing is just spam.
觉得不错,分享给更多人看到
Linux中国 微信二维码
分享这篇文章
7月30日 21:51
Linux中国 最新头条文章
Linux中国 热门头条文章

我要回帖

更多关于 python 调用另一个py 的文章

 

随机推荐