zoukankan      html  css  js  c++  java
  • python--python脚本中调用shell命令

    文章目录
    python脚本调用shell命令
    os.system()
    os.popen()
    subprocess.call()
    subprocess.Popen()
    commands
    python脚本传参数给shell命令
    python脚本获取命令行用户输入参数
    python传参数给shell命令
    python传参数给shell脚本
    参考: Python调用shell命令常用方法

    python脚本调用shell命令
    os.system()
    os.system()执行成功会自动返回值0,执行的shell命令结果会打印出来,例如执行如下代码:

    import os

    if __name__ == "__main__":

    print("this is a test file ")

    cmd = "ls -a"
    val = os.system(cmd)
    print(val)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    结果如下所示,显示当前文件目录下的所有文件简略信息,val打印的结果是0,表明执行成功。如果不成功,返回的结果是1。

    $ python test2.py
    this is a test file
    . .. learn-to-pack mean_shift.py 'pyspark demo.py' test.py test2.py
    0
    1
    2
    3
    4
    os.popen()
    os.popen()以文件的形式返回shell运行结果,通常需要用read()或者readlines()读取。

    import os

    if __name__ == "__main__":

    print("this is a test file ")

    cmd = "ls"
    val = os.popen(cmd)
    print(val)
    print(val.read())
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    上述代码运行结果如下所示,可以看到直接打印val是不能看到信息的,采用read()可以读取其中的内容。

    $ python test2.py
    this is a test file
    <os._wrap_close object at 0x000002511FC8CC88>
    learn-to-pack
    mean_shift.py
    pyspark demo.py
    test.py
    test2.py
    1
    2
    3
    4
    5
    6
    7
    8
    如果采用readlines(),就需要用循环的方式一行一行读取了。结果与上面一样。

    for tmp in val.readlines():
    print(tmp,end='')
    1
    2
    subprocess.call()
    subprocess.call()功能类似os.system(),返回值表示执行成功与否。

    import subprocess as sp

    if __name__ == "__main__":

    print("this is a test file ")

    cmd = "ls"
    val = sp.call(cmd)
    print(val)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    执行结果如下,返回值0表示执行成功。

    $ python test2.py
    this is a test file
    learn-to-pack mean_shift.py 'pyspark demo.py' test.py test2.py
    0
    1
    2
    3
    4
    subprocess.Popen()
    参考:python中的subprocess.Popen()使用

    import subprocess as sp

    if __name__ == "__main__":

    print("this is a test file ")

    cmd = "ls"
    res = sp.Popen(cmd, shell=True, stdout=sp.PIPE, stderr=sp.STDOUT)
    visible_res = res.stdout.readlines()
    for tmp in visible_res:
    print(tmp.decode(), end=' ')
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    执行结果如下。subprocess.Popen()需要讲的太多了,先占个坑…

    $ python test2.py
    this is a test file
    learn-to-pack
    mean_shift.py
    pyspark demo.py
    test.py
    test2.py
    1
    2
    3
    4
    5
    6
    7
    看到有博客提到 shlex.split() 可以用来格式化字符串,按照空格将其分割处理。原始的字符串中可能有多个空格的情况,处理之后都可以正确分割。分割之后的值可以直接送到 sp.Popen() 运行。

    cmd = "ls -a | grep test"
    cmd = shlex.split(cmd)
    print(cmd)
    1
    2
    3
    打印结果为:

    ['ls', '-a', '|', 'grep', 'test']
    1
    commands
    该模块在python3中已经删除了。。。

    python脚本传参数给shell命令
    这里涉及到三个问题,第一是python脚本获取命令行用户输入参数,第二是python脚本中直接执行的shell命令如何获取python代码的参数,第三是python脚本中执行的是shell脚本,其如何获取python传递的参数。下面分别依次说明。

    python脚本获取命令行用户输入参数
    这里可以参考我的另一篇博客:python–获取参数输入(获取用户输入)

    python传参数给shell命令
    代码如下,传递一个路径 s_path 给 ls 命令,直接用字符串连接的形式,注意命令之间的空格。

    import subprocess as sp

    if __name__ == "__main__":

    print("this is a test file ")

    s_path = "learn-to-pack"
    cmd = "ls " + s_path + " | grep test"
    res = sp.Popen(cmd, shell=True, stdout=sp.PIPE, stderr=sp.STDOUT)
    visible_res = res.stdout.readlines()
    for tmp in visible_res:
    print(tmp.decode(),end=' ')
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    执行结果:

    $ python test2.py
    this is a test file
    test1.py
    1
    2
    3
    python传参数给shell脚本
    代码如下,传参形式与前面的一样。

    import subprocess as sp

    if __name__ == "__main__":

    print("this is a test file ")

    s_path = "learn-to-pack"
    cmd = "sh " + "E:/code-study/shell/test1.sh " + s_path
    res = sp.Popen(cmd, shell=True, stdout=sp.PIPE, stderr=sp.STDOUT)
    visible_res = res.stdout.readlines()
    for tmp in visible_res:
    print(tmp.decode(),end=' ')
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    而test1.sh中的代码如下,shell中通过$1读取参数,类似在命令行执行shell一样。

    echo $1
    echo "the data path is $1"
    ls $1
    data_path=$1
    echo path
    echo $data_path
    1
    2
    3
    4
    5
    6
    python脚本的执行结果为:

    $ python test2.py
    this is a test file
    learn-to-pack
    the data path is learn-to-pack
    __pycache__
    build
    dist
    liuying.py
    liuying.spec
    test1.py
    path
    learn-to-pack
    ————————————————
    转自:https://blog.csdn.net/liuyingying0418/article/details/100366971

  • 相关阅读:
    miniNExT
    使用ExaBGP发送BGP路由信息和清洗DDoS流量
    HTML day02(html列表与菜单的制作)
    HTML day01基础总结
    SSH项目整合基本步骤
    常见异常类有哪些?
    JSP 生命周期
    HTTP状态码
    使用oracle删除表中重复记录
    Oracle三种分页?
  • 原文地址:https://www.cnblogs.com/fw-qql/p/14695011.html
Copyright © 2011-2022 走看看