簡單了解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’)
另外一個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() 這個方法會打開一個管道,返回結(jié)果是一個連接管道的文件對象,該文件對象的操作方法同open(),可以從該文件對象中讀取返回結(jié)果。如果執(zhí)行成功,不會返回狀態(tài)碼,如果執(zhí)行失敗,則會將錯誤信息輸出到stdout,并返回一個空字符串。這里官方也表示subprocess模塊已經(jīng)實(shí)現(xiàn)了更為強(qiáng)大的subprocess.Popen()方法。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Nginx+php配置文件及原理解析2. Intellij IDEA 2019 最新亂碼問題及解決必殺技(必看篇)3. Android Manifest中meta-data擴(kuò)展元素數(shù)據(jù)的配置與獲取方式4. java中throws實(shí)例用法詳解5. CSS3實(shí)現(xiàn)動態(tài)翻牌效果 仿百度貼吧3D翻牌一次動畫特效6. 關(guān)于HTML5的img標(biāo)簽7. Android自定義View實(shí)現(xiàn)掃描效果8. PHP5.0正式發(fā)布 不完全兼容PHP4 新增多項(xiàng)功能9. css3溢出隱藏的方法10. ASP.NET MVC獲取多級類別組合下的產(chǎn)品
