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 ...
随机推荐
- XSLT格式
XSL 指扩展样式表语言(EXtensible Stylesheet Language). XSL - 不仅仅是样式表语言,包括三部分: XSLT :一种用于转换 XML 文档的语言. XPath : ...
- mysql - InnoDB存储引擎 死锁问题( Deadlock found when trying to get lock; try restarting transaction )
刚刚向数据库插入数据的时候出现了这么一段错误 Deadlock found when trying to get lock; try restarting transaction 主要原因(由于无法使 ...
- POJ-2115-C Looooops(线性同余方程)
链接: https://vjudge.net/problem/POJ-2115 题意: A Compiler Mystery: We are given a C-language style for ...
- transitionend事件 监听 fadeIn fadeOut 两个方法无效(动画结束时无法执行transitionend里面的代码)
//下面的例子证明 fadeIn() fadeOut() 不能使用transitionend事件进行监听. //说白了在fadeIn fadeOut 后面监听动画结束时,transitionend是不 ...
- LBA逻辑块地址
LBA简介 磁盘读取发展 IO操作读取硬盘的三种方式: chs方式 :小于8G (8064MB) LBA28方式:小于137GB LBA48方式:小于144,000,000 GB LBA方式访问使用了 ...
- Jedis API操作redis数据库
1.配置文件 classpath路径下,新建redis.properties配置文件 配置文件内容 # Redis settings redis.host=127.0.0.1 redis.port=6 ...
- nginx做反向代理时出现302错误
现象:nginx在使用非80端口做反向代理时,浏览器访问发现返回302错误 详细现象如下: 浏览器请求登录页: 输入账号密码点击登录: 很明显登录后跳转的地址少了端口号. 原因:proxy.conf文 ...
- Mac之Sublime Text使用Go
安装Golang build 包 点击 Preferences > Package control 菜单(MAC快捷键 shift + command + p) 在弹出的输入框输入 instal ...
- RK3399 4G模块移远EC20移植调试
转载请注明出处:https://www.cnblogs.com/lialong1st/p/11266330.html CPU:RK3399 系统:Android 7.1 1.通过串口打印或者adb获取 ...
- EL表达式 与 JSTL标准标签库
目录 EL表达式 什么是EL表达式 作用 EL内置11对象 EL执行表达式 JSTL 什么是JSTL JSTL标准标签库有5个子库 把JSTL标签库jar包引入工程当中 if标签 foreach标签 ...