zoukankan      html  css  js  c++  java
  • 110_01 subprocess模块(了解)

    一、subprocess模块

    subprocess模块允许你去创建一个新的进程让其执行另外的程序,并与它进行通信,获取标准的输入、标准输出、标准错误以及返回码等。更多查看官网:https://docs.python.org/2/library/subprocess.html?highlight=subprocess#frequently-used-arguments

    import subprocess
    import subprocess
    '''
    sh-3.2# ls /Users/nick/Desktop |grep txt$
    mysql.txt
    tt.txt
    事物.txt
    '''
    
    res1 = subprocess.Popen('ls /Users/jieli/Desktop',
                            shell=True,
                            stdout=subprocess.PIPE)
    res = subprocess.Popen('grep txt$',
                           shell=True,
                           stdin=res1.stdout,
                           stdout=subprocess.PIPE)
    
    print(res.stdout.read().decode('utf-8'))
    
    # 等同于上面,但是上面的优势在于,一个数据流可以和另外一个数据流交互,可以通过爬虫得到结果然后交给grep
    res1 = subprocess.Popen('ls /Users/jieli/Desktop |grep txt$',
                            shell=True,
                            stdout=subprocess.PIPE)
    print(res1.stdout.read().decode('utf-8'))
    
    # windows下:
    # dir | findstr 'test*'
    # dir | findstr 'txt$'
    res1 = subprocess.Popen(r'dirC:\Users\Administrator\PycharmProjects\test\函数备课',
                            shell=True,
                            stdout=subprocess.PIPE)
    res = subprocess.Popen('findstr test*',
                           shell=True,
                           stdin=res1.stdout,
                           stdout=subprocess.PIPE)
    
    # subprocess使用当前系统默认编码,得到结果为bytes类型,在windows下需要用gbk解码
    print(res.stdout.read().decode('gbk'))
    
  • 相关阅读:
    软件测试人员的要求
    冒烟测试和回归测试的区别
    [go]struct
    [go]socket编程
    [go]gorhill/cronexpr用go实现crontab
    [go]os/exec执行shell命令
    [go]time包
    [go]etcd使用
    [go]redis基本使用
    [go]go操作mysql
  • 原文地址:https://www.cnblogs.com/abdm-989/p/11858721.html
Copyright © 2011-2022 走看看