http://blog.csdn.net/pipisorry/article/details/46972171

Python经常被称作“胶水语言”,因为它能够轻易地操作其他程序,轻易地包装使用其他语言编写的库。在Python/wxPython环境下,执行外部命令或者说在Python程序中启动另一个程序的方法。

1、os.system(command)

os.system()函数用来运行shell命令。此命令可以方便的调用或执行其他脚本和命令

  • #打开指定的文件
  • >>>os.system('notepad *.txt')
  • 这个调用相当直接,且是同步进行的,程序需要阻塞并等待返回。返回值是依赖于系统的,直接返回系统的调用返回值,所以windows和linux是不一样的。

    如果filename字符串中有空格,则会出现错误:File Not Found错误

    可以通过下面[

    
    

    [Python中subprocess学习]

    4、webbrowser.open(url)

    前面三个方法只能用于执行程序和打开文件,不能处理URL,打开URL地址可用webbrowser模块提供的功能。

    调用系统缺省浏览器打开URL地址,如 webbrowser.open('http://www.jb51.net'),也可以利用
    webbrowser.open('h:\python.zip')来执行程序。这样可以不必区分是文件名还是URL,不知道在Linux下是否可行。
    以上在Windows2000,Python2.4a1,wxPython 2.5.1运行。
    [python调用shell的方法]

    5. os.popen(command[,mode[,bufsize]])

    举个栗子

    >>> import os
    >>> p = os.popen("dir c:", 'r')
    >>> p.read()
    bla bla... <这里是dir正确的输出>
    >>> p.close()
    >>> p = os.popen("dir d:", 'r') # 电脑中没有D盘
    >>> p.read()
    ''
    >>> p.close()
    1
    >>>

    可以看出,popen方法通过p.read()获取终端输出,而且popen需要关闭close().当执行成功时,close()不返回任何值,失败时,close()返回系统返回值. 可见它获取返回值的方式和os.system不同。

    6. 使用commands模块

    举个栗子

    >>> import commands
    >>> commands.getstatusoutput('ls /bin/ls')
    (0, '/bin/ls')
    >>> commands.getstatusoutput('cat /bin/junk')
    (256, 'cat: /bin/junk: No such file or directory')
    >>> commands.getstatusoutput('/bin/junk')
    (256, 'sh: /bin/junk: not found')
    >>> commands.getoutput('ls /bin/ls')
    '/bin/ls'
    >>> commands.getstatus('/bin/ls')
    '-rwxr-xr-x 1 root 13352 Oct 14 1994 /bin/ls'

    根据你需要的不同,commands模块有三个方法可供选择。getstatusoutput, getoutput, getstatus。

    皮皮blog

    7. Python文档中目前全力推荐第四个方法subprocess

    当我们运行python的时候,我们都是在创建并运行一个进程。正如我们在Linux进程基础中介绍的那样,一个进程可以fork一个子进程,并让这个子进程exec另外一个程序。在Python中,我们通过标准库中的subprocess包来fork一个子进程,并运行一个外部的程序(fork,exec见Linux进程基础)。

    subprocess包中定义有数个创建子进程的函数,这些函数分别以不同的方式创建子进程,所以我们可以根据需要来从中选取一个使用。另外subprocess还提供了一些管理标准流(standard stream)和管道(pipe)的工具,从而在进程间使用文本通信。

    使用subprocess包中的函数创建子进程的时候,要注意:

    1) 在创建子进程之后,父进程是否暂停,并等待子进程运行。

    2) 函数返回什么

    3) 当returncode不为0时,父进程如何处理。

    直接调用命令,返回值即是系统返回。

    shell命令中有一些是shell的内建命令,这些命令必须通过shell运行,$cd。shell=True允许我们运行这样一些命令。shell=True表示命令最终在shell中运行。Python文档中出于安全考虑,不建议使用shell=True。建议使用Python库来代替shell命令,或使用pipe的一些功能做一些转义。:

    例子

    import subprocess
    new_filename = '/tmp/b'
    filename = '/tmp/a'
    subprocess.call(['mv', filename, new_filename])
    subprocess.call([
    1. 启动virtualenv是一个shell命令,要在shell中执行。否则出错:WindowsError: [Error 193] %1 is not a valid Win32 application 2.  然而virtualenv中执行的命令并不是shell命令,不是在shell中执行的,直接运行即可。否则出错:'scrapy' is not recognized as an internal or external command,operable program or batch file.
    subprocess.call(r'..\Scripts\activate', shell=True)
    subprocess.call('scrapy crawl dmoz')

    如果你更关注命令的终端输出,可以这样

    >>> subprocess.check_output(["echo", "Hello World!"])
    'Hello World!\n'

    >>> subprocess.check_output("exit 1", shell=True)

    Traceback (most recent call last):
    ...
    subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

    python3.5最新语法及示例

    subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None, shell=False, timeout=None, check=False)

    >>> subprocess.run(["ls", "-l"])  # doesn't capture output
    CompletedProcess(args=['ls', '-l'], returncode=0)
    
    >>> subprocess.run("exit 1", shell=True, check=True)
    Traceback (most recent call last):
      ...
    subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
    
    >>> subprocess.run(["ls", "-l", "/dev/null"], stdout=subprocess.PIPE)
    CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
    stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n')

    更复杂的命令使用,如

    subprocess.run( "cat ~/files/DATASETS/tianchi/ccf_data_revised/ccf_online_stage1_train.csv | cut -d ',' -f 1 | uniq | sort -n > /tmp/1.txt", shell=True)

    subprocess.run(r"mv '" + os.path.join(CA_DATASET_DIR, checkin_ca) + "' /tmp", shell=True)

    参数使用

    stdin和stdout

    import subprocess
    child1 = subprocess.Popen(["ls", "-l"], stdout=subprocess.PIPE, cwd='/home/pipi')
    child2 = subprocess.Popen(["cat"], stdin=child1.stdout, shell=True)
    相当于将child1输出保存到一个文件,再使用child2的cat命令查看
    

    input

    The input argument is passed to Popen.communicate() and thus to thesubprocess’s stdin. If used it must be a byte sequence, or a string ifuniversal_newlines=True. When used, the internal Popen objectis automatically created with stdin=PIPE, and the stdin argument maynot be used as well.

    input相当于一个文件中的内容,而不是命令的参数!

    child2 = subprocess.run(["cat"], input = b'contents in a file', shell=True) # contents in a file
    child2 = subprocess.run(["echo"], input = b'contents in a file', shell=True) # 无输出

    使用代码中的变量

    好像可以通过stdin输入代码中的变量,但是更简单的也可以直接使用下面的字符串连接实现

    dir = '/home/pipi/files/DATASETS/tianchi/ccf_data_revised'
    train_filename = 'data_train2.txt'
    subprocess.run("cat " + train_filename + " | cut -d ',' -f 9 | sort| uniq -c", shell=True, cwd=dir)

    指定子进程工作路径

    Note: 上一条run修改当前路径对下一条run是没用的

    dir = '/home/pipi/files/DATASETS/tianchi/ccf_data_revised'
    subprocess.run("cat data_train2.txt | cut -d ',' -f 9 | sort| uniq -c", shell=True, cwd=dir)

    获取Popen的返回值及输出

    import sys,os,subprocess,commands
    from subprocess import Popen,PIPE
    p = Popen('python ' + path + '\getCurPath.py', stdout=PIPE, stderr=PIPE)
    p.wait()
    if(p.returncode == 0):
        print "stdout:%s" %p.stdout.read()

    subprocess还有另一种更简单方法,效果一样,它会返回stdout

    s = subprocess.check_output('ls -l', shell=True)

    [subprocess — Subprocess management]

    [Python标准库06 子进程 (subprocess包)]

    [Python模块整理(三):子进程模块subprocess]

    8. 还有两种方法:os.spawn* 和 popen2.*。它们也可以完成同样的任务

    Python和其他进程的管道通信方式--popen和popen2的比较

    9. sh : Python subprocess interface

    lz发现了一个相当不错的库amoffat/sh,可以用 Python 函数的语法去调用 shell 命令,sh 之于 subprocess 类似 requests 之于 urllib2。

    pip3 install sh

    sh库用来将shell命令作为函数导入到Python中。在bash中使用是非常实用的,但是在Python中不容易记住怎么使用(即递归搜索文件)。

    使用示例

    from sh import find
    find("/tmp")
    /tmp/foo
    /tmp/foo/file1.json
    /tmp/foo/file2.json
    /tmp/foo/file3.json
    /tmp/foo/bar/file3.json

    from sh import git
    git.clone("https://github.com/amoffat/sh")

    from sh import ls, mv
    
    new_filename = r'/tmp/b'
    filename = r'/tmp/a'
    try:
        mv(filename, new_filename)
    except:
        pass
    print(ls('/tmp'))

    [sh:sh 1.08 — sh v1.08 documentation]

    [python sh库实现分析]

    from:http://blog.csdn.net/pipisorry/article/details/46972171

    ref:python中执行linux命令(调用linux命令)

    6方法,python中执行shell命令

    python模块:调用系统命令模块subprocess等的更多相关文章

    1. python常用模块-调用系统命令模块(subprocess)

      python常用模块-调用系统命令模块(subprocess) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. subproces基本上就是为了取代os.system和os.spaw ...

    2. 调用系统命令之subprocess模块

      除了常见的os.system和os.popen方法,官方强烈推荐使用subprocess来调用系统命令. 这个库用起来其实很简单,按照惯例先贴一下官文关键点: The subprocess modul ...

    3. 在Python中调用C++模块

      一.一般调用流程 http://www.cnblogs.com/huangshujia/p/4394276.html 二.Python读取图像并传入C++函数,再从C++返回结果图像给Python h ...

    4. Python多层目录模块调用

      一. 引用模块在 父+级目录中: 1. 将导入模块所在目录(../model/模块)添加到系统环境变量path下,可添加多个 import syssys.path.append("../mo ...

    5. thinkphp 跨模块调用

      5.13 跨模块调用   在开发过程中经常会在当前模块调用其他模块的方法,这个时候就涉及到跨模块调用,我们还可以了解到A和R两个快捷方法的使用.例如,我们在Index模块调用User模块的操作方法 c ...

    6. ThinkPHP 跨模块调用操作方法(A方法与R方法)

      ThinkPHP 跨模块调用操作方法(A方法与R方法) 跨模块调用操作方法 前面说了可以使用 $this 来调用当前模块内的方法,但实际情况中还经常会在当前模块调用其他模块的方法.ThinkPHP 内 ...

    7. thinkphp跨模块调用

      thinkphp跨模块调用 跨模块调用模板 return $view->fetch('admin@user/add'); 全路径模板调用: return $view->fetch(APP_ ...

    8. 4.1 python中调用rust程序

      概述 使用rust-cpython将rust程序做为python模块调用: 通常为了提高python的性能: 参考 https://github.com/dgrunwald/rust-cpython ...

    9. Python 调用系统命令的模块 Subprocess

      Python 调用系统命令的模块 Subprocess 有些时候需要调用系统内部的一些命令,或者给某个应用命令传不定参数时可以使用该模块. 初识 Subprocess 模块 Subprocess 模块 ...

    随机推荐

    1. C实战:强大的程序调试工具GDB

      C实战:强大的程序调试工具GDB 1.基本调试 这里只列举最最常用的GDB命令. 1.1 启动GDB gdb program:准备调试程序.也可以直接进入gdb,再通过file命令加载. 1.2 添加 ...

    2. Dynamics CRM2016 在实体命名时需要注意的事项

      在使用web api的过程中遇到个很无语的设置,体现在对实体名的设置上,之前看到accounts以为只是在实体名上加个s,也没往深处看,但真正进入项目实施了问题就来了,city直接变成了cities不 ...

    3. Dynamics CRM2013 Form利用window.location.reload()进行全局刷新带来的问题及解决办法

      CRM2013以后,表单的保存后变成了局部刷新而非全局刷新,但很多情况下我们需要刷新整个页面,通过刷新页面来使脚本执行或者业务规则执行来实现某些业务效果,一般我们会使用window.location. ...

    4. Android的SharedPreferences(首选项)保存键值对

      使用共享首选项 如果您有想要保存的相对较小键值集合,您应使用 SharedPreferences API.SharedPreferences 对象指向包含键值对的文件并提供读写这些文件的简单方法. 每 ...

    5. Android基于Retrofit2.0 +RxJava 封装的超好用的RetrofitClient工具类(六)

      csdn :码小白 原文地址: http://blog.csdn.net/sk719887916/article/details/51958010 RetrofitClient 基于Retrofit2 ...

    6. 在代码中写view 的长宽高等

      获得资源的id的另一种方法 int layoutRes = getResources().getIdentifier("pager_view" + i, "layout& ...

    7. qsort函数应用大全

      七种qsort排序方法 <本文中排序都是采用的从小到大排序> 一.对int类型数组排序  int num[100]; Sample: int cmp ( const void *a , c ...

    8. python辅助开发模块(非官方)如pil,mysqldb,openpyxl,xlrd,xlwd

      官方文档 只是支持win32, 不支持win64 所以很麻烦 民间高人,集中做了一堆辅助库,下载后,用python安装目录下的scripts中,pip和easy_install就可以安装了 pytho ...

    9. ORACLE数据库学习之数据库的优化

       数据库的优化 概述 影响数据库性能的因素包括:系统.数据库.网络. 数据库的优化包括:优化数据库磁盘I/O.优化回滚段.优化Rrdo日志.优化系统全局区.优化数据库对象. 监控数据库的性能: 在 ...

    10. 剑指Offer——分治算法

      剑指Offer--分治算法 基本概念 在计算机科学中,分治法是一种很重要的算法.字面上的解释是"分而治之",就是把一个复杂的问题分成两个或更多的相同或相似的子问题,再把子问题分成更 ...