>>> import os
>>> os.system('ls')
blog.tar djangoblog.log managerblog.sh test.txt
data ENV nginx-1.15.11 wget-log
0
提示:python3已结废弃
例子:
创新互联是一家专业提供那曲企业网站建设,专注与成都网站制作、成都网站设计、外贸营销网站建设、HTML5建站、小程序制作等业务。10年已为那曲众多企业、政府机构等服务。创新互联专业网络公司优惠进行中。
>>> import os
>>> os.popen('ls')
<os._wrap_close object at 0x7f0acd548b00>
>>> os.popen('ls').read()
'blog.tar\ndata\ndb.sql\ndead.letter\nDjangoBlog\ndjangoblog.log\nENV\nfile1\nget-pip.py\nindex.html\nmanagerblog.sh\nnginx-1.15.11\nPython-3.5.2\nstartDjangoBlog.sh\nstart_uwsgi.ini\ntest.txt\nwget-log\nzrlog-2.1.3-b5f0d63-release.war?attname=ROOT.war\n'
>>> os.popen('ls').read().split('\n')
['blog.tar', 'data', 'db.sql', 'dead.letter', 'DjangoBlog', 'djangoblog.log', 'ENV', 'file1', 'get-pip.py', 'index.html', 'managerblog.sh', 'nginx-1.15.11', 'Python-3.5.2', 'startDjangoBlog.sh', 'start_uwsgi.ini', 'test.txt', 'wget-log', 'zrlog-2.1.3-b5f0d63-release.war?attname=ROOT.war', '']
获取命令执行的结果,但是没有命令的执行状态,这样可以将获取的结果保存起来放到list中。
>>> import subprocess
>>> status,result=subprocess.getstatusoutput('ls')
>>> status
0
>>> result
'blog.tar\ndata\ndb.sql\ndead.letter\nDjangoBlog\ndjangoblog.log\nENV\nfile1\nget-pip.py\nindex.html\nmanagerblog.sh\nnginx-1.15.11\nPython-3.5.2\nstartDjangoBlog.sh\nstart_uwsgi.ini\ntest.txt\nwget-log\nzrlog-2.1.3-b5f0d63-release.war?attname=ROOT.war'
>>> result.split('\n')
['blog.tar', 'data', 'db.sql', 'dead.letter', 'DjangoBlog', 'djangoblog.log', 'ENV', 'file1', 'get-pip.py', 'index.html', 'managerblog.sh', 'nginx-1.15.11', 'Python-3.5.2', 'startDjangoBlog.sh', 'start_uwsgi.ini', 'test.txt', 'wget-log', 'zrlog-2.1.3-b5f0d63-release.war?attname=ROOT.war']
2、getoutput
>>> print(subprocess.getoutput('ls').split('\n') )
['blog.tar', 'data', 'db.sql', 'dead.letter', 'DjangoBlog', 'djangoblog.log', 'ENV', 'file1', 'get-pip.py', 'index.html', 'managerblog.sh', 'nginx-1.15.11', 'Python-3.5.2', 'startDjangoBlog.sh', 'start_uwsgi.ini', 'test.txt', 'wget-log', 'zrlog-2.1.3-b5f0d63-release.war?attname=ROOT.war']
3、call
执行命令,返回状态码(命令正常执行返回0,报错则返回1)
>>> subprocess.call('ls')
blog.tar djangoblog.log managerblog.sh test.txt
data ENV nginx-1.15.11 wget-log
db.sql file1 Python-3.5.2 zrlog-2.1.3-b5f0d63-release.war?attname=ROOT.war
dead.letter get-pip.py startDjangoBlog.sh
DjangoBlog index.html start_uwsgi.ini
0
4、check_call
执行命令,如果执行成功则返回状态码0,否则抛异常
>>> subprocess.check_call('ls')
blog.tar djangoblog.log managerblog.sh test.txt
data ENV nginx-1.15.11 wget-log
db.sql file1 Python-3.5.2 zrlog-2.1.3-b5f0d63-release.war?attname=ROOT.war
dead.letter get-pip.py startDjangoBlog.sh
DjangoBlog index.html start_uwsgi.ini
0
>>> subprocess.check_call('ls luojun')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/python3/lib/python3.5/subprocess.py", line 576, in check_call
retcode = call(*popenargs, **kwargs)
File "/usr/local/python3/lib/python3.5/subprocess.py", line 557, in call
with Popen(*popenargs, **kwargs) as p:
File "/usr/local/python3/lib/python3.5/subprocess.py", line 947, in __init__
restore_signals, start_new_session)
File "/usr/local/python3/lib/python3.5/subprocess.py", line 1551, in _execute_child
raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'ls luojun'
5、check_output
执行命令,如果执行成功则返回执行结果,否则抛异常
>>> subprocess.check_output('ls')
b'blog.tar\ndata\ndb.sql\ndead.letter\nDjangoBlog\ndjangoblog.log\nENV\nfile1\nget-pip.py\nindex.html\nmanagerblog.sh\nnginx-1.15.11\nPython-3.5.2\nstartDjangoBlog.sh\nstart_uwsgi.ini\ntest.txt\nwget-log\nzrlog-2.1.3-b5f0d63-release.war?attname=ROOT.war\n'
>>> subprocess.check_output('ls luojun')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/python3/lib/python3.5/subprocess.py", line 626, in check_output
**kwargs).stdout
File "/usr/local/python3/lib/python3.5/subprocess.py", line 693, in run
with Popen(*popenargs, **kwargs) as process:
File "/usr/local/python3/lib/python3.5/subprocess.py", line 947, in __init__
restore_signals, start_new_session)
File "/usr/local/python3/lib/python3.5/subprocess.py", line 1551, in _execute_child
raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'ls luojun'
6、subprocess.Popen(…)
用于执行复杂的系统命令
参数 注释
args shell命令,可以是字符串或者序列类型(如:list,元组)
bufsize 指定缓冲。0 无缓冲,1 行缓冲,其他 缓冲区大小,负值 系统缓冲
stdin, stdout, stderr 分别表示程序的标准输入、输出、错误句柄
preexec_fn 只在Unix平台下有效,用于指定一个可执行对象(callable object),它将在子进程运行之前被调用
close_sfs 在windows平台下,如果close_fds被设置为True,则新创建的子进程将不会继承父进程的输入、输出、错误管道。所以不能将close_fds设置为True同时重定向子进程的标准输入、输出与错误(stdin, stdout, stderr)。
shell 同上
cwd 用于设置子进程的当前目录
env 用于指定子进程的环境变量。如果env = None,子进程的环境变量将从父进程中继承。
universal_newlines 不同系统的换行符不同,True -> 同意使用 \n
startupinfo 只在windows下有效,将被传递给底层的CreateProcess()函数,用于设置子进程的一些属性,如:主窗口的外观,进程的优先级等等
createionflags 同上
[root@VM_0_2_centos test]# vim test.sh
[root@VM_0_2_centos test]# cat test.sh
#!/bin/bash
echo "this is my test shell ${1} ${2}"
exit 0
[root@VM_0_2_centos test]# chmod +x test.sh
[root@VM_0_2_centos test]# /test/test.sh jluo jluocc.cn
this is my test shell jluo jluocc.cn
[root@VM_0_2_centos test]# vim mytest.py
[root@VM_0_2_centos test]# cat mytest.py
#! /usr/bin/env python3
import os
import sys
if(len(sys.argv)<3):
print("please input two arguments")
sys.exit(1)
arg0 = sys.argv[1]
arg1 = sys.argv[2]
print('=====file name *.py全路径=====')
print(sys.argv[0])
print('=====脚本执行结果如下=====')
os.system('/test/test.sh '+arg0+' '+arg1)
[root@VM_0_2_centos test]# python3 mytest.py jluo jluocc.cn
=====file name *.py全路径=====
mytest.py
=====脚本执行结果如下=====
this is my test shell jluo jluocc.cn
[root@VM_0_2_centos test]# python3 /test/mytest.py jluo jluocc.cn
=====file name *.py全路径=====
/test/mytest.py
=====脚本执行结果如下=====
this is my test shell jluo jluocc.cn
结束语:
更多精彩内容持续更新中,关注微信公众号,有你更精彩。
分享名称:python和shell的结合与应用
URL网址:https://www.cdcxhl.com/article42/ppdsec.html
成都网站建设公司_创新互联,为您提供网页设计公司、外贸网站建设、网站策划、关键词优化、静态网站、做网站
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联