python调用shell命令
1、subprocess介绍
官方推荐 subprocess模块,os.system(command) 这个废弃了
亲测 os.system 使用sed需要进行字符转义,非常麻烦
python3 subprocess模块使用
2、subprocess模块使用
官网说明文档 subprocess.call 和 subprocess.check_call执行命令,返回状态码。两者唯一的区别在于返回值。执行成功,都返回 0;执行失败,check_call 将raise出来CalledProcessError。
2.1、subprocess.call () 使用
import subprocess
subprocess.call(['ls', '-l'])
subprocess.call(['ls -l'], shell=True)
注意shell=True 的用法,支持命令以字符串的形式传入。
2.2、subprocess.getoutput()使用
以字符串的形式返回执行结果,错误或者正确都会返回,命令本身不会报错
import subprocess
a = subprocess.getoutput('ls /Users/wangxiansheng/desktops')
print(a)
print(type(a))
ls: /Users/wangxiansheng/desktops: No such file or directory
<class 'str'>
变量 a 保存了错误信息并返回。
subprocess.getstatusoutput(cmd)
返回包含执行结果状态码和输出信息的元组。
import subprocess
a = subprocess.getstatusoutput('ls /Users/wangxiansheng/desktops')
print(a)
print(type(a))
(1, 'ls: /Users/wangxiansheng/desktops: No such file or directory')
<class 'tuple'>
3、python调用sed
方式一:
>>> subprocess.call(["sed","-i",r"s/hello/hi/g",'/home/b.sh'])
0
方式二:
>>> subprocess.call(["sed -i 's/hello/hi/g' /home/b.sh"], shell=True)
0
4、python调用grep
>>> subprocess.call(["cat d.txt | grep hello"], shell=True)
hello
0
>>> subprocess.call(["grep -n 'word' /root/d.txt"], shell=True)
2:word
0
5、python调用awk
>>> subprocess.call(["cat d.txt | awk '{print $1}'"], shell=True)
hello
word
come
on
0
6、python调用find
>>> subprocess.call(['find / -name b.sh'], shell=True)
/home/b.sh
0
7、subprocess怎么判断命令是否执行成功
>>> import subprocess
>>> if subprocess.call(["lss"], shell=True) !=0:
... print('Without the command')
...
/bin/sh: lss: command not found
Without the command
在pycharm中调用shell命令
1、
# -*- coding:UTF-8 -*-
import subprocess
subprocess.call(["ls /home"], shell=True)
#subprocess.call(["cat /root/d.txt | grep hello"], shell=True)
执行结果
ssh://root@192.168.0.75:22/usr/bin/python -u /app/py_code/test3.py
b.sh oracle test
2、
# -*- coding:UTF-8 -*-
import subprocess
subprocess.call(["cat /home/b.sh"], shell=True)
subprocess.call(["sed -i 's/hi/hello/g' /home/b.sh"], shell=True)
subprocess.call(["cat /home/b.sh"], shell=True)
执行结果
ssh://root@192.168.0.75:22/usr/bin/python -u /app/py_code/test3.py
hi
hello
参考文档
https://blog.csdn.net/u010454729/article/details/46640083
https://blog.csdn.net/CityzenOldwang/article/details/78382960
https://www.cnblogs.com/xiaozhiqi/p/5814564.html
https://www.jb51.net/article/141877.htm
http://www.cnblogs.com/tomato0906/articles/4605114.html os.system(command) 废弃了
https://cloud.tencent.com/developer/news/257058
python调用shell命令的更多相关文章
- python 调用 shell 命令方法
python调用shell命令方法 1.os.system(cmd) 缺点:不能获取返回值 2.os.popen(cmd) 要得到命令的输出内容,只需再调用下read()或readlines()等 ...
- python 调用shell命令三种方法
#!/usr/bin/python是告诉操作系统执行这个脚本的时候,调用/usr/bin下的python解释器: #!/usr/bin/env python这种用法是为了防止操作系统用户没有将pyth ...
- python 调用 shell 命令
记录 python 调用 shell 命令的方法 加载 os 模块, 使用 os 类 import os; os.system("ls /");
- 用Python调用Shell命令
Python经常被称作“胶水语言”,因为它能够轻易地操作其他程序,轻易地包装使用其他语言编写的库,也当然可以用Python调用Shell命令. 用Python调用Shell命令有如下几种方式: 第一种 ...
- python 调用shell命令的方法
在python程序中调用shell命令,是件很酷且常用的事情…… 1. os.system(command) 此函数会启动子进程,在子进程中执行command,并返回command命令执行完毕后的退出 ...
- Python 调用 Shell命令
python程序中调用shell命令,是件很酷且常用的事情今天来总结一下 1.使用os模块 的 system 此函数会启动子进程,在子进程中执行command,并返回comman ...
- Python调用shell命令常用方法
Python调用shell指令 方法一.使用os模块的system方法:os.system(cmd),其返回值是shell指令运行后返回的状态码,int类型,0表示shell指令成功执行,256表示未 ...
- (转载)python调用shell命令之os 、commands、subprocess
linux系统下进入python交互式环境: 一.os 模块 1.1.os模块的exec方法簇: python交互界面中: In [1]: import os In [2]: os.exec os.e ...
- python调用shell命令之三慷慨法
preface: 忙于近期的任务,须要用到libsvm的一些命令.如在终端执行java svm_train train_file model_file. pythonsubset.py file tr ...
随机推荐
- [hdoj6483][莫队+线段树/ST]
A Sequence Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- 02_View
1.View 1.基于类的视图 Class-based Views REST framework提供APIView是Django的View的子类 发送到View的Request请求:是REST fra ...
- Mysql存储过程查询数据插入别的表里。
DELIMITER// CREATE PROCEDURE setRoomManger2() BEGIN ); ; DECLARE cur CURSOR FOR SELECT roomid FROM n ...
- HTML怎么块外横向剧中
HTML 块外横向剧中 在HTML中有一个块外横向剧中的代码 那就是margin:0 auto 这个能是块内元素横向剧中 剧中前: 剧中后
- 四十一.redis主从复制 RDB/AOF持久化 数据类型
把redis集群里的主机 恢复为独立的redis服务器(51-58) ]# redis-cli -h 192.168.4.51 -p 6351 shutdown ]# rm -rf /var/lib ...
- luogu 5505 [JSOI2011]分特产 广义容斥
共有 $m$ 种物品,每个物品 $a[i]$ 个,分给 $n$ 个人,每个人至少要拿到一件,求方案数. 令 $f[i]$ 表示钦定 $i$ 个没分到特产,其余 $(n-i)$ 个人随便选的方案数,$g ...
- P1833 樱花
题目背景 <爱与愁的故事第四弹·plant>第一章. 题目描述 爱与愁大神后院里种了n棵樱花树,每棵都有美学值Ci.爱与愁大神在每天上学前都会来赏花.爱与愁大神可是生物学霸,他懂得如何欣赏 ...
- payOrder
parent <script> export default class Parents extends wepy.page { data = { tabdata:{}, //下面要用这里 ...
- 数据结构实验之图论三:判断可达性(SDUT 2138)(简单DFS)
#include <bits/stdc++.h> using namespace std; int gra[1002][1005]; int vis[1002]; int n,m; voi ...
- 2019-12-11:kali linux工具Msfvenom 命令自动补全
msfvenom大家都不陌生,在我们使用MSF进行权限维持,内网渗透的时候都会用到,支持的语言的种类很多.大家都知道我们在使用msfvenom 的时候需要手动输入很多参数,这些参数需要记忆,或记在其它 ...