3种python调用其他脚本的方法,你还知道其他的方法吗?
生活随笔
收集整理的這篇文章主要介紹了
3种python调用其他脚本的方法,你还知道其他的方法吗?
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.用python調用python腳本
#!/usr/local/bin/python3.7 import time import oscount = 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') 1 2運行結果:
[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 Bye2.python調用shell方法os.system()
''' 遇到問題沒人解答?小編創建了一個Python學習交流QQ群:778463939 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' #!/usr/local/bin/python3.7 import time import oscount = 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" 1 2 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 Bye3.python調用shell方法os.popen()
''' 遇到問題沒人解答?小編創建了一個Python學習交流QQ群:778463939 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' #!/usr/local/bin/python3.7 import time import oscount = 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 Byeos.system.popen()這個方法會打開一個管道,返回結果是一個連接管道的文件對象,該文件對象的操作方法同open(),可以從該文件對象中讀取返回結果。如果執行成功,不會返回狀態碼,如果執行失敗,則會將錯誤信息輸出到stdout,并返回一個空字符串。這里官方也表示subprocess模塊已經實現了更為強大的subprocess.Popen()方法。
總結
以上是生活随笔為你收集整理的3种python调用其他脚本的方法,你还知道其他的方法吗?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python线程join和setDaem
- 下一篇: 如何在python中使用正则表达式从多行