您好,登錄后才能下訂單哦!
怎么在python中調(diào)用其他腳本?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
1.用python調(diào)用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')
運行結(jié)果:
[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調(diào)用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"
運行結(jié)果:
[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調(diào)用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')
運行結(jié)果:
[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() 這個方法會打開一個管道,返回結(jié)果是一個連接管道的文件對象,該文件對象的操作方法同open(),可以從該文件對象中讀取返回結(jié)果。如果執(zhí)行成功,不會返回狀態(tài)碼,如果執(zhí)行失敗,則會將錯誤信息輸出到stdout,并返回一個空字符串。這里官方也表示subprocess模塊已經(jīng)實現(xiàn)了更為強大的subprocess.Popen()方法。
看完上述內(nèi)容,你們掌握怎么在python中調(diào)用其他腳本的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。