python中的shell是什么

608次阅读
没有评论

python中的shell是什么

我们在给系统下达命令时,可以通过shell的方法来实现,也就是我们常说的命令行。比较特殊的是,它接受的是键盘输入的命令。本篇在对shell进行介绍的同时,还带来了两种执行shell命令的方法,一起来看看吧。

python shell说明

当谈到命令行时,我们实际上指的是shell。

shell是一个接受由键盘输入的命令,并将其传递给操作系统来执行的程序。

python shell执行方法

(1)commands模块

commands对Python的os.popen()进行了封装,使用SHELL命令字符串作为其参数,返回命令的结果数据以及命令执行的状态;该命令目前已经废弃,被subprocess所替代。

import commands
a,b = commands.getstatusoutput('ls')
a是退出状态
b是输出的结果。
>>> import commands
>>> a,b = commands.getstatusoutput('ls')
>>> print a
0
>>> print b
anaconda-ks.cfg
install.log
install.log.syslog

(2)subprocess模块

Python目前已经废弃了os.system,os.spawn*,os.popen*,popen2.*,commands.*来执行其他语言的命令,subprocesss是被推荐的方法;

subprocess允许你能创建很多子进程,创建的时候能指定子进程和子进程的输入、输出、错误输出管道,执行后能获取输出结果和执行状态。

import subprocess
subprocess.call(command, shell=True) 会直接打印出结果。
subprocess.Popen(command, shell=True) 也可以是subprocess.Popen(command, stdout=subprocess.PIPE, shell=True) 这样就可以输出结果了。

以上就是python中shell的有关介绍,大家可以就两种命令行方法进行尝试python shell吧,希望对初学python的人有所帮助。

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2021-05-01发表,共计908字。
新手QQ群:570568346,欢迎进群讨论 Python51学习