簡單了解python調(diào)用其他腳本方法實(shí)例
1.用python調(diào)用python腳本
#!/usr/local/bin/python3.7import timeimport os count = 0str = (’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’)
另外一個(gè)python腳本b.py如下:
#!/usr/local/bin/python3.7print(’hello world’)
運(yùn)行結(jié)果:
[python@master2 while]$ python a.py hello worldthis count is: 1this count is: 2this count is: 3this count is: 4this count is: 5this count is: 6this count is: 7this count is: 8Good Bye
2.python調(diào)用shell方法os.system()
#!/usr/local/bin/python3.7import timeimport os count = 0n = 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/shecho 'hello world'
運(yùn)行結(jié)果:
[python@master2 while]$ python a.py hello worldthis count is: 1this count is: 2this count is: 3this count is: 4this count is: 5this count is: 6this count is: 7this count is: 8Good Bye
3.python調(diào)用shell方法os.popen()
#!/usr/local/bin/python3.7import timeimport os count = 0n = 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’)
運(yùn)行結(jié)果:
[python@master2 while]$ python a.py <os._wrap_close object at 0x7f7f89377940>[’hello worldn’]this count is: 1this count is: 2this count is: 3this count is: 4this count is: 5this count is: 6this count is: 7this count is: 8Good Bye
os.system.popen() 這個(gè)方法會(huì)打開一個(gè)管道,返回結(jié)果是一個(gè)連接管道的文件對(duì)象,該文件對(duì)象的操作方法同open(),可以從該文件對(duì)象中讀取返回結(jié)果。如果執(zhí)行成功,不會(huì)返回狀態(tài)碼,如果執(zhí)行失敗,則會(huì)將錯(cuò)誤信息輸出到stdout,并返回一個(gè)空字符串。這里官方也表示subprocess模塊已經(jīng)實(shí)現(xiàn)了更為強(qiáng)大的subprocess.Popen()方法。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. moment轉(zhuǎn)化時(shí)間戳出現(xiàn)Invalid Date的問題及解決2. python爬蟲實(shí)戰(zhàn)之制作屬于自己的一個(gè)IP代理模塊3. WML的簡單例子及編輯、測(cè)試方法第1/2頁4. Ajax返回值類型與用法實(shí)例分析5. .NET6打包部署到Windows Service的全過程6. 解決ajax請(qǐng)求后臺(tái),有時(shí)收不到返回值的問題7. 開發(fā)效率翻倍的Web API使用技巧8. asp批量添加修改刪除操作示例代碼9. 使用JSP技術(shù)實(shí)現(xiàn)一個(gè)簡單的在線測(cè)試系統(tǒng)的實(shí)例詳解10. HTML 絕對(duì)路徑與相對(duì)路徑概念詳細(xì)
