知行编程网知行编程网  2022-11-23 06:30 知行编程网 隐藏边栏  21 
文章评分 0 次,平均分 0.0
导语: 本文主要介绍了关于如何调用python中的shell脚本?的相关知识,包括python调取shell脚本,以及python控制shell命令这些编程知识,希望对大家有参考作用。


我相信每个人都应该对这方面有很好的了解。小编在前几期一直在教大家相关内容。我不知道我现在是否向你提及它。脑海中会不会有些印象?或者,大家能不能完成我们文章给小编的问题呢?至少你心里应该知道怎么调用一些脚本,然后根据自己的想象,阅读下面小编给出的内容,组合优化


~


1.



python调用shell方法os.system()

#!/usr/local/bin/python3.7
import time
import os
 
count = 0
n = os.system('sh b.sh')
while True:
    count = count + 1
    if count == 8:
      print('this count is:',count)
      break
    else:
      time.sleep(1)
      print('this count is:',count)   
 
print('Good Bye')



shell脚本如下:

#!/bin/sh
echo "hello world"



运行结果:

[python@master2 while]$ python a.py
hello world
this count is: 1
this count is: 2
this count is: 3
this count is: 4
this count is: 5
this count is: 6
this count is: 7
this count is: 8
Good Bye


2.



python调用shell方法os.popen()

#!/usr/local/bin/python3.7
import time
import os
 
count = 0
n = os.system('sh b.sh')
while True:
    count = count + 1
    if count == 8:
      print('this count is:',count)
      break
    else:
      time.sleep(1)
      print('this count is:',count)   
 
print('Good Bye')



运行结果:

[python@master2 while]$ python a.py
<os._wrap_close object at 0x7f7f89377940>
['hello world\n']
this count is: 1
this count is: 2
this count is: 3
this count is: 4
this count is: 5
this count is: 6
this count is: 7
this count is: 8
Good Bye



好吧,我们看看和通常的调用方式有什么不同?大概可以看出,大约

python调用脚本时候,大致都是这样子的吧,那大家如果遇到类似问题,可以举一反三学习哦~

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写
扫一扫二维码分享