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命令的更多相关文章

  1. python 调用 shell 命令方法

    python调用shell命令方法 1.os.system(cmd) 缺点:不能获取返回值 2.os.popen(cmd) 要得到命令的输出内容,只需再调用下read()或readlines()等   ...

  2. python 调用shell命令三种方法

    #!/usr/bin/python是告诉操作系统执行这个脚本的时候,调用/usr/bin下的python解释器: #!/usr/bin/env python这种用法是为了防止操作系统用户没有将pyth ...

  3. python 调用 shell 命令

    记录 python 调用 shell 命令的方法 加载 os 模块, 使用 os 类 import os; os.system("ls /");

  4. 用Python调用Shell命令

    Python经常被称作“胶水语言”,因为它能够轻易地操作其他程序,轻易地包装使用其他语言编写的库,也当然可以用Python调用Shell命令. 用Python调用Shell命令有如下几种方式: 第一种 ...

  5. python 调用shell命令的方法

    在python程序中调用shell命令,是件很酷且常用的事情…… 1. os.system(command) 此函数会启动子进程,在子进程中执行command,并返回command命令执行完毕后的退出 ...

  6. Python 调用 Shell命令

    python程序中调用shell命令,是件很酷且常用的事情今天来总结一下   1.使用os模块 的  system         此函数会启动子进程,在子进程中执行command,并返回comman ...

  7. Python调用shell命令常用方法

    Python调用shell指令 方法一.使用os模块的system方法:os.system(cmd),其返回值是shell指令运行后返回的状态码,int类型,0表示shell指令成功执行,256表示未 ...

  8. (转载)python调用shell命令之os 、commands、subprocess

    linux系统下进入python交互式环境: 一.os 模块 1.1.os模块的exec方法簇: python交互界面中: In [1]: import os In [2]: os.exec os.e ...

  9. python调用shell命令之三慷慨法

    preface: 忙于近期的任务,须要用到libsvm的一些命令.如在终端执行java svm_train train_file model_file. pythonsubset.py file tr ...

随机推荐

  1. plsql工具使用

    作者:lhrbest 作者:张冲andy 作者:jack_孟

  2. ckeditor编辑器从word粘贴公式

    由于工作需要必须将word文档内容粘贴到编辑器中使用 但发现word中的图片粘贴后变成了file:///xxxx.jpg这种内容,如果上传到服务器后其他人也访问不了,网上找了很多编辑器发现没有一个能直 ...

  3. The 2019 ICPC China Nanchang National Invitational and International Silk-Road Programming Contest

    目录 Contest Info Solutions A. Attack B. Polynomial E. Interesting Trip F. Sequence G. Winner H. Anoth ...

  4. linux系列(一):ls命令

    ls命令是linux下最常用的命令.ls命令就是list的缩写,默认下ls用来打印出当前目录的清单,如果ls指定其他目录,那么就会显示指定目录里的文件及文件夹清单. 通过ls 命令不仅可以查看linu ...

  5. css添加了原始滚动条要隐藏滚动条的显示

    // 添加伪类 <style> ::-webkit-scrollbar {display:none} </style> <div style=" width:2 ...

  6. docker hub 国内镜像加速地址

    当前可用 配置文件:vim /etc/docker/daemon.json { "registry-mirrors" : [ "http://docker.mirrors ...

  7. ROS模拟

    亲测,在古月大大这篇博客中的一条命令最好改为rostopic pub /cmd_vel geometry/Twist -r 10 -- '[0.2,0,0]' '[0,0,0.5]'. http:// ...

  8. codeforces#1251E2. Voting (Hard Version)(贪心)

    题目链接: http://codeforces.com/contest/1251/problem/E2 题意: 主角需要获得n个人的投票 有两种方式让某个人投票 1,已经投票的人数大于m 2,花p枚硬 ...

  9. CF2B The least round way(贪心+动规)

    题目 CF2B The least round way 做法 后面\(0\)的个数,\(2\)和\(5\)是\(10\)分解质因数 则把方格中的每个数分解成\(2\)和\(5\),对\(2\)和\(5 ...

  10. 【SPOJ】Distinct Substrings

    [SPOJ]Distinct Substrings 求不同子串数量 统计每个点有效的字符串数量(第一次出现的) \(\sum\limits_{now=1}^{nod}now.longest-paren ...