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 ...
随机推荐
- 安装Genymotion模拟器(第三方)
优势: 启动速度更快 注册账户,下载可用的系统镜像,就可以使用. 官方网站: https://www.genymotion.com/account/login/ 选择的版本是带VirtualB ...
- element-ui upload组件 onchange事件 传自定义参数
<el-upload class="upload-demo" list-type="picture" accept=" ...
- Robot Framework--RIDE面板与库的说明
Robot Framework的测试用例是以project作为单位进行管理的.一个project可以包含多个Test Suite文件,每一个Test Suite可以包含多条测试用例一个Test Sui ...
- MOS管做开关之初理解
杰杰 物联网IoT开发 2017-10-12 大家好,我是杰杰. 今晚,设计电路搞了一晚上,终于从模电渣渣的我,把MOS管理解了那么一丢丢,如有写的不好的地方,请指出来.谢谢. ...
- [cf1140D. Minimum Triangulation][dp]
D. Minimum Triangulation time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Shared Nothing、Shared Everthting、Shared Disk
数据库构架设计中主要有Shared Everthting.Shared Nothing.和Shared Disk:1.Shared Everything:一般是针对单个主机,完全透明共享CPU/MEM ...
- TensorFlow(十五):使用inception-v3实现各种图像识别
上代码: import tensorflow as tf import os import numpy as np import re from PIL import Image import mat ...
- linux系列(十三):less命令
1.命令格式: less [参数] 文件 2.命令功能: less 与 more 类似,但使用 less 可以随意浏览文件,而 more 的功能不如less,而且 less 在查看之前不会加载整个文 ...
- linux桌面发行版简介
本文通过MetaWeblog自动发布,原文及更新链接:https://extendswind.top/posts/technical/linux_desktop_distribution linux系 ...
- Pytest从测试类外为测试用例动态注入数据
今天Nelly问我Pytest能不能支持从TestClass类外传入参数?从类外批量传入各个test方法需要的参数.因为数据文件可能有很多情况,不方便依次匹配. 然而又必须用类对用例进行归类及复用,数 ...