subprocess模块可以允许我们执行shell命令

一般来说,使用run()方法就可以满足大部分情况

  1. 使用run执行shell命令

    In [5]: subprocess.run('echo "hello"',shell=True)
    helloOut[5]: CompletedProcess(args='echo "hello"', returncode=0)

  2. run方法执行的命令都是在子shell中进行的
    # shell.py的内容import subprocess
    subprocess.run('cd /',shell=True)

    当我们执行python shell.py时,并不会进入根目录

  3. 在特定目录下执行run方法,给run方法传入cwd参数
    subprocess.run('ls',shell=True,cwd='/')

    这条命令会在根目录下执行

转载于:https://www.cnblogs.com/time-read/p/8306378.html

python的subprocess模块执行shell命令相关推荐

  1. python3中使用subprocess模块执行外部命令

    一. subprocess模块介绍 subprocess模块可以替代os模块下的os.system和os.popen等操作方法 subprocess模块在python2和python3上的使用上有一定 ...

  2. python3使用subprocess_python3中使用subprocess模块执行外部命令

    一. subprocess模块介绍 1. subprocess模块可以替代os模块下的os.system和os.popen等操作方法 2. subprocess模块在python2和python3上的 ...

  3. Python subprocess.check_output 执行shell命令 返回结果(单次执行shell命令)

    Python3中的subprocess.check_output函数可以执行一条sh命令,并返回命令的输出内容,用法如下: output = subprocess.check_output([&quo ...

  4. Python编程:sh模块执行shell命令

    文档: http://amoffat.github.io/sh/index.html 安装 pip install sh 简单示例 >>> import sh >>> ...

  5. python3 subprocess.check_output 执行shell命令 返回结果

    Python3中的subprocess.check_output函数可以执行一条sh命令,并返回命令的输出内容,用法如下: output = subprocess.check_output([&quo ...

  6. python中command是什么意思_python中command执行shell命令脚本方法

    在Python中有一个模块commands也很容易做到以上的效果. 看一下三个函数: 1). commands.getstatusoutput(cmd) 用os.popen()执行命令cmd, 然后返 ...

  7. python commands模块_python之commands和subprocess入门介绍(可执行shell命令的模块)

    一.commands模块 1.介绍 当我们使用Python进行编码的时候,但是又想运行一些shell命令,去创建文件夹.移动文件等等操作时,我们可以使用一些Python库去执行shell命令. com ...

  8. python commands执行不连续_[Python] 利用commands模块执行Linux shell命令

    用Python写运维脚本时,经常需要执行linux shell的命令,Python中的commands模块专门用于调用Linux shell命令,并返回状态和结果,下面是commands模块的3个主要 ...

  9. python调用shell命令-在Python中执行shell命令的6种方法,你都知道吗?

    原标题:在Python中执行shell命令的6种方法,你都知道吗? Python经常被称作"胶水语言",因为它能够轻易地操作其他程序,轻易地包装使用其他语言编写的库.今天我们就讲解 ...

最新文章

  1. PAT 1074 Reversing Linked List
  2. 最新wingide6破解方法(支持Linux),亲测有效,支持python3.0
  3. 计算机 专业英语 考试,《计算机专业英语》期末考试试卷
  4. SAP 使用ST05跟踪方法
  5. java怎么设置快速修复键_Java开发环境之------MyEclipse快捷键和排除错误第一选择ctrl+1(***重点***:ctrl+1,快速修复---有点像vs中的快速using...
  6. 开发转运维有什么好点的理由_芜湖好点的团购社区费用
  7. Python中第三方的库(library)、模块(module),包(package)的安装方法以及ImportError: No module named...
  8. 从attention到Transformer+CV中的self-attention
  9. java中匿名类的注意细节
  10. 阿里P9专家右军:大话软件质量稳定性
  11. python组合数据类型选择题_python基础学习——基础数据类型练习题(二)
  12. ionic4 hammerjs手势事件左滑右滑
  13. 微型计算机及接口技术考试卷,微机原理与接口技术考试试题及答案 2
  14. 1117 Eddington Number
  15. linux docker自动启动,linux – Cron作业不能在Docker容器内自动运行
  16. Fiddler4 抓取Chrome浏览器的Http(s)
  17. 狂神SSM项目整合(含完整代码免费)
  18. 独家深访:腾讯变革150天全记录
  19. mysql全量备份脚本_mysql全量备份脚本
  20. html表格自动分列,CSS3 Columns分列式布局方法简介

热门文章

  1. IO对象不可以复制或者赋值
  2. 三数之和(Java、C实现)
  3. 利用正则匹配数字后边的字符_图解正则——字符匹配
  4. 搭建Apache Mina框架并实现Server与Client端的简单消息传递
  5. JSP教程(八)—— Servlet实现验证码
  6. 面试题55 字符流中第一个不重复的字符
  7. Oracle Length 和 Lengthb 函数说明 .(用来判断记录值里是否有中文内容)
  8. FXK Javascript
  9. DSP2812程序执行过程
  10. 对比kCCPositionTypeFree和kCCPositionTypeRelative两种粒子移动类型