- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章日常整理python执行系统命令的常见方法(全)由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
具体内容如下:
1 os.system 。
例如 ipython中运行如下命令,返回运行状态status 。
os.system('cat /etc/passwdqc.conf') min=disabled,24,11,8,7 max=40 passphrase=3 match=4 similar=deny random=47 enforce=everyone retry=3 Out[6]: 0 。
2 os.popen() 。
popen(command [, mode='r' [, bufsize]]) -> pipe Open a pipe to/from a command returning a file object. 。
运行返回结果 。
In [20]: output = os.popen('cat /proc/cpuinfo') In [21]: lineLen = [] In [22]: for line in output.readlines(): lineLen.append(len(line)) ....: In [23]: line line lineLen In [23]: lineLen Out[23]: [14, 25, ... 。
3 如何同时返回结果和运行状态,commands模块
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#String form: <module 'commands' from '/usr/lib64/python2.7/commands.pyc'>
File
:
/
usr
/
lib64
/
python2.
7
/
commands.py
Docstring:
Execute shell commands via os.popen()
and
return
status, output.
Interface summary:
import
commands
outtext
=
commands.getoutput(cmd)
(exitstatus, outtext)
=
commands.getstatusoutput(cmd)
outtext
=
commands.getstatus(
file
)
# returns output of "ls -ld file"
A trailing newline
is
removed
from
the output string.
Encapsulates the basic operation:
pipe
=
os.popen(
'{ '
+
cmd
+
'; } 2>&1'
,
'r'
)
text
=
pipe.read()
sts
=
pipe.close()
|
commands示例如下
In [24]: (status, output) = commands.getstatusoutput('cat /proc/cpuinfo') In [25]: status Out[25]: 0 In [26]: len(output) Out[26]: 3859 。
4 使用模块subprocess 。
ipython 中运行"?subprocess"可以发现subprocess是python用来替换os.popen()等管道操作命令的新模块 。
A more real-world example would look like this
1
2
3
4
5
6
7
8
|
try
:
retcode
=
call(
"mycmd"
+
" myarg"
, shell
=
True
)
if
retcode <
0
:
print
>>sys.stderr,
"Child was terminated by signal"
,
-
retcode
else
:
print
>>sys.stderr,
"Child returned"
, retcode
except
OSError, e:
print
>>sys.stderr,
"Execution failed:"
, e
|
相对于上面几种方式,subprocess便于控制和监控进程运行结果,subprocess提供多种函数便于应对父进程对子进程不同要求
4.1.1 subprocess.call() 。
父进程父进程等待子进程完成,返回exit code 。
4.1.2 subprocess.check_call() 。
父进程等待子进程完成,返回0,如果returncode不为0,则举出错误subprocess.CalledProcessError,该对象包含有returncode属性,可用try...except...来检查 。
4.1.3 subprocess.check_output() 。
父进程等待子进程完成 。
返回子进程向标准输出的输出结果 。
检查退出信息,如果returncode不为0,则举出错误subprocess.CalledProcessError,该对象包含有returncode属性和output属性,output属性为标准输出的输出结果,可用try...except...来检查 。
例如
In [32]: out = subprocess.call("ls -l", shell=True) total 42244 -rw-rw-r--. 1 *** *** 366 May 26 09:10 ChangeLog 。
4.2.1 。
上面三个函数都是源于Popen()函数的wapper(封装),如果需要更加个性化应用,那么就需要使用popen()函数 。
Popen对象创建后,主程序不会自动等待子进程完成。我们必须调用对象的wait()方法,父进程才会等待 (也就是阻塞block) 。
1
2
3
4
5
6
7
8
|
[wenwt@localhost syntax]$ rm subprocess.pyc
[wenwt@localhost syntax]$ python process.py
parent process
[wenwt@localhost syntax]$ PING www.google.com (
173.194
.
219.99
)
56
(
84
) bytes of data.
^C
[wenwt@localhost syntax]$
-
-
-
www.google.com ping statistics
-
-
-
5
packets transmitted,
0
received,
100
%
packet loss, time
3999ms
|
加上wait方法
1
2
3
4
5
|
[wenwt@localhost syntax]$ python process.py
PING www.google.com (
173.194
.
219.103
)
56
(
84
) bytes of data.
-
-
-
www.google.com ping statistics
-
-
-
5
packets transmitted,
0
received,
100
%
packet loss, time
3999ms
parent process
|
补充介绍:Python 执行终端命令的方法 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
import
os
import
subprocess
'''
os.system模块
os.system("ls -hl") 执行命令并返回状态码,当返回0表示成功;返回256表示失败,痛点是无法返回output
os.popen模块
os.popen("ls -hl") 执行命令,之后通过.read()方法获取output返回值
subprocess模块
subprocess.getstatusoutput("ls -hl") 执行命令,并返回状态status、输出output
subprocess.getoutput("ls -hl") 执行命令,只返回输出结果output
subprocess.call("ls -hl") 执行命令并返回状态码 和os.system("ls -hl")类似
'''
def
test_system(cmd):
status
=
os.system(cmd)
# 会自动输出output到控制台 但是无法接收,status为0表示成功、status为256表示失败
print
(status)
def
test_popen(cmd):
output
=
os.popen(cmd).read()
# 只会获取到命令的output,如果是有output的错误命令 会输出output,否则输出空白
print
(output)
def
test_getoutput(cmd):
output
=
subprocess.getoutput(cmd)
# 和os.popen(cmd)类似
print
(output)
def
test_getstatusoutput(cmd):
status, output
=
subprocess.getstatusoutput(cmd)
# 执行命令,并返回状态status、输出output
print
(status)
print
(output)
def
test_call(cmd):
status
=
subprocess.call(cmd)
# 和os.system(cmd)类似
print
(status)
if
__name__
=
=
'__main__'
:
# test_system('ls -lh') # test_system('test')
# test_popen('pwd') # test_popen('test')
# test_getoutput('pwd')
# test_getstatusoutput('pwd')
test_call(
'pwd'
)
|
以上内容就是本文的全部叙述,希望大家喜欢.
最后此篇关于日常整理python执行系统命令的常见方法(全)的文章就讲到这里了,如果你想了解更多关于日常整理python执行系统命令的常见方法(全)的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
Apache服务器全局配置之服务器标识配置篇 服务器标识相关指令: ServerName ServerAdmin ServerSignature ServerTokens UseCanonical
//校验是否全由数字组成 ? 1
具体内容如下: 1 os.system 例如 ipython中运行如下命令,返回运行状态status os.system('cat /etc/passwdqc.conf') min=disab
基本操作 查看数据库 ? 1
Xcode使用教程详细讲解是本文要介绍的内容,Xcode是一个款强大的IDE开发环境,就像你在写Windows程序时需要VS2005一样 需要要Xcode为你写Mac程序提供环境。因此,如果你要成为
就如今天遇到随即函数rand();脑海中想到用它做点啥好呢,最后想起了验证码,数字验证码,字母验证码,中文验证码,可是自己不会呀,咋办呢,上网搜,看别人的代码,开不懂,看视频,听老师讲,将其中所遇到
pcre-7.8.tar.gz 正则表达式下载地址:ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/
IDEA常用设置(提高开发效率) 本人也是IDEA编译器的忠实用户了,但是有时出于各种原因,比如更换设备等等,IDEA总是需要重新安装配置。这就让我比较苦恼,因为总是记不全自己之前都修改了
? 1 2
1、 操作环境搭建 系统:Windows7 旗舰版 64位 PHP环境:wampserver2.2e-php5.4.3-httpd2.2.22-mysql5.5.24 32
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 去年关闭。 Improve th
我已经为我在 Adobe Fireworks Cs5 中构建的页面生成了 css,我想知道如何为我的标题和其他 div 设置完整宽度。我将显示标题部分的 css 代码。 @charset "utf
您好,我希望表单宽度为 100%。我希望文本框几乎延伸整个页面的宽度,并在文本框的右侧直接放置小的“GO”按钮,所有按钮都在同一行(或 block )上。 现在我的文本框只有其中文本的宽度(“在此处输
我没有设法将全宽页脚粘贴到网页底部。当页脚上方的主要内容低于一定高度时,页脚下方有一个空白区域。我尝试使用各种解决方案,例如以下 css 代码: html,body { margin:0; padd
我想要一个全宽的表格。当我给 position:fixed; 它变成全宽但可滚动不起作用。 简而言之,我需要一个与浏览器主体没有任何边距的表格。 body { font-family: "Helv
我注意到很多大型网站(如 Google 和 Facebook)在查看页面源代码时 99% 的源代码都是 JavaScript。 有人知道这种方法相对于常规 HTML+JavaScript 页面的优势吗
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this q
由于某种原因,我网站上的图片不再是全宽的。 据我所见,所有内容都设置为 100% 宽度。 http://cargocollective.com/btatest 我是不是什么地方都没有? 最佳答案 我认
我正在创建一个菜单并尝试使用 CSS 制作全宽菜单。但是,我不确定菜单项将如何出现在菜单中。 这是问题的截图: 问题出在我得到的“GAP”中。 如果我有固定数量的元素,我知道我可以使用这个逻辑: ul
这个问题在这里已经有了答案: Puzzle: Find largest rectangle (maximal rectangle problem) (6 个答案) 关闭 9 年前。 给定一个二元矩阵
我是一名优秀的程序员,十分优秀!