subprocess是用来fork一个子进程的。这个子进程可以运行一个外部程序。

函数:

  • subprocess.call()
  • subprocess.check_output()
  • subprocess.check_call()

这三个函数都调用Popen函数:因此Popen类的初始化函数的入参,都可以通过被上面三个函数使用

def check_output(*popenargs, **kwargs):
r"""Run command with arguments and return its output as a byte string. If the exit code was non-zero it raises a CalledProcessError. The
CalledProcessError object will have the return code in the returncode
attribute and output in the output attribute. The arguments are the same as for the Popen constructor. Example: >>> check_output(["ls", "-l", "/dev/null"])
'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n' The stdout argument is not allowed as it is used internally.
To capture standard error in the result, use stderr=STDOUT. >>> check_output(["/bin/sh", "-c",
... "ls -l non_existent_file ; exit 0"],
... stderr=STDOUT)
'ls: non_existent_file: No such file or directory\n'
"""
if 'stdout' in kwargs:
raise ValueError('stdout argument not allowed, it will be overridden.')
process = Popen(stdout=PIPE, *popenargs, **kwargs)
class Popen(object):
_child_created = False # Set here since __del__ checks it def __init__(self, args, bufsize=0, executable=None,
stdin=None, stdout=None, stderr=None,
preexec_fn=None, close_fds=False, shell=False,
cwd=None, env=None, universal_newlines=False,
startupinfo=None, creationflags=0):

举例:app_module_list = subprocess.check_output("grep '\[INFO\] cn.tong' dependency.log | awk '{print $2}' | awk -F':' '{print $2}'", shell=True, cwd=dir)

进入到目录dir后执行shell命令:grep '\[INFO\] cn.tong' dependency.log | awk '{print $2}' | awk -F':' '{print $2}',标准输出stdout赋值给app_module_list

参考:https://blog.csdn.net/bitcarmanlee/article/details/51622263

subprocess使用,进入到某个目录下执行shell命令的更多相关文章

  1. mysq在命令行模式下执行shell命令

    mysql可以在命令行模式下执行shell命令 mysql> help For information about MySQL products and services, visit: htt ...

  2. node.js在Linux下执行shell命令、.sh脚本

    首先,引入子进程模块 var process = require('child_process'); 执行shell命令 调用该模块暴露出来的方法exec process.exec('shutdown ...

  3. [开源项目]Shell4Win,一个在Windows下执行shell命令的解释器

    背景 顺利拿到心目中的理想offer之后,心里的负担一下减轻了很多,希望利用还没毕业之前这段难得的悠闲时间做一点有意义的事情.于是希望能做一个长久以来都想做的开源项目,就是题中提到的Windows下的 ...

  4. [转帖]Linux /tmp目录下执行脚本失败提示Permission denied

    Linux /tmp目录下执行脚本失败提示Permission denied https://www.cnblogs.com/linyfeng/p/11087655.html 国产化的环境上 就有一个 ...

  5. python的subprocess模块执行shell命令

    subprocess模块可以允许我们执行shell命令 一般来说,使用run()方法就可以满足大部分情况 使用run执行shell命令 In [5]: subprocess.run('echo &qu ...

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

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

  7. Linux下执行ls命令提示CMake Error错误

    一.系统环境 Fedora10 二.出错情况 执行ls命令出现如下错误提示: CMake Error: The source directory "/etc/--color=auto&quo ...

  8. Windows环境下执行hadoop命令出现Error: JAVA_HOME is incorrectly set Please update D:\SoftWare\hadoop-2.6.0\conf\hadoop-env.cmd错误的解决办法(图文详解)

    不多说,直接上干货! 导读   win下安装hadoop 大家,别小看win下的安装大数据组件和使用  玩过dubbo和disconf的朋友们,都知道,在win下安装zookeeper是经常的事   ...

  9. linux在home目录下使用ls命令卡死

    linux在home目录下使用ls命令卡死,原因可能是mount的某个服务器挂掉或出啥问题了,这个时候umount掉就正常了,如果umount提示device is busy,这时可以使用强制卸载   ...

随机推荐

  1. mysql五-2:多表查询

    一 介绍 本节主题 多表连接查询 复合条件连接查询 子查询 准备表 company.employeecompany.department #建表 create table department( id ...

  2. Buildroot自动化交叉编译工具:其一【转】

    转自:http://blog.csdn.net/youyudehexie/article/details/7583657 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] Bui ...

  3. python读取doc

    import os, time, fnmatch from docx import Document class search: def __init__(self, path, search_str ...

  4. vue+axios下载文件的实现

    HTML代码: <el-button size="medium" @click="download">下载表格</el-button> ...

  5. eclipse启动几秒后报错 (一闪而过)

    eclipse启动报错,让查看.metadata/.log日志   1 !SESSION 2013-09-23 17:28:28.484 ------------------------------- ...

  6. python的加密算法(1):反转加密

    说白了,就是把字符串倒序. 在js里,有一个reverse.但是python中没有. 不过,有一个更简单的方法,就是: ‘abcd’ [::-1] 这里,具体解释一下: (参看:https://doc ...

  7. 关于oracle的sqlplus的另一些小技巧

    执行脚本的命令在上一节已经讲过,不再重复. sqlplus user/password@ip:port/servicename @/path/sqltest.sql; sqltest的内容及注释: - ...

  8. [BZOJ4006][JLOI2015]管道连接 状压dp+斯坦纳树

    4006: [JLOI2015]管道连接 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 1020  Solved: 552[Submit][Statu ...

  9. QTP自动化测试框架的基础知识

    1. 什么是自动化测试框架? 假定你有一个活,需要构建许多自动化测试用例来测试这个应用程序.当你对这个应用程序完成自动化测试后,你对自己创建脚本应该有什么期望吗?你难道不想要- 脚本应该按照预期的来执 ...

  10. HDU 6330.Problem L. Visual Cube-模拟到上天-输出立方体 (2018 Multi-University Training Contest 3 1012)

    6330.Problem L. Visual Cube 这个题就是输出立方体.当时写完怎么都不过,后来输出b<c的情况,发现这里写挫了,判断失误.加了点东西就过了,mdzz... 代码: //1 ...