3種python調用其他腳本的方法
1.用python調用python腳本
#!/usr/local/bin/python3.7 import time import os count = 0 str = ('python b.py') result1 = os.system(str) print(result1) 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腳本b.py如下:
#!/usr/local/bin/python3.7 print('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.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
3.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
os.system.popen() 這個方法會打開一個管道,返回結果是一個連接管道的文件對象,該文件對象的操作方法同open(),可以從該文件對象中讀取返回結果。如果執(zhí)行成功,不會返回狀態(tài)碼,如果執(zhí)行失敗,則會將錯誤信息輸出到stdout,并返回一個空字符串。這里官方也表示subprocess模塊已經(jīng)實現(xiàn)了更為強大的subprocess.Popen()方法。
總結
以上所述是小編給大家介紹的3種python調用其他腳本的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!
相關文章
Python實現(xiàn)獲取網(wǎng)站PR及百度權重
這篇文章主要介紹了Python實現(xiàn)獲取網(wǎng)站PR及百度權重,本文使用傳參的方式請求站長工具和谷歌工具獲取PR值和百度權重,需要的朋友可以參考下2015-01-01詳解Python list和numpy array的存儲和讀取方法
這篇文章主要介紹了詳解Python list和numpy array的存儲和讀取方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11使用python來調用CAN通訊的DLL實現(xiàn)方法
今天小編就為大家分享一篇使用python來調用CAN通訊的DLL實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07詳解Python 重學requests發(fā)起請求的基本方式
這篇文章主要介紹了詳解Python 重學requests發(fā)起請求的基本方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-02-02