python--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)

结果如下所示,显示当前文件目录下的所有文件简略信息,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

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())

上述代码运行结果如下所示,可以看到直接打印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

如果采用readlines(),就需要用循环的方式一行一行读取了。结果与上面一样。

for tmp in val.readlines():
        print(tmp,end='')

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)

执行结果如下,返回值0表示执行成功。

$ python test2.py
this is a test file
 learn-to-pack   mean_shift.py  'pyspark demo.py'   test.py   test2.py
0

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=' ')

执行结果如下。subprocess.Popen()需要讲的太多了,先占个坑…

$ python test2.py
this is a test file
learn-to-pack
 mean_shift.py
 pyspark demo.py
 test.py
 test2.py

看到有博客提到 shlex.split() 可以用来格式化字符串,按照空格将其分割处理。原始的字符串中可能有多个空格的情况,处理之后都可以正确分割。分割之后的值可以直接送到 sp.Popen() 运行。

cmd = "ls  -a | grep test"
cmd = shlex.split(cmd)
print(cmd)

打印结果为:

['ls', '-a', '|', 'grep', 'test']

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=' ')

执行结果:

$ python test2.py
this is a test file
test1.py

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=' ')

而test1.sh中的代码如下,shell中通过$1读取参数,类似在命令行执行shell一样。

echo $1
echo "the data path is $1"
ls $1
data_path=$1
echo path
echo $data_path

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
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值