簡單了解python調(diào)用其他腳本方法實例
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)了更為強(qiáng)大的subprocess.Popen()方法。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python 二叉樹的層序建立與三種遍歷實現(xiàn)詳解
這篇文章主要介紹了Python 二叉樹的層序建立與三種遍歷實現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-07-07pytorch 實現(xiàn)tensor與numpy數(shù)組轉(zhuǎn)換
今天小編就為大家分享一篇使用pytorch 實現(xiàn)tensor與numpy數(shù)組轉(zhuǎn)換,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12利用python和ffmpeg 批量將其他圖片轉(zhuǎn)換為.yuv格式的方法
今天小編就為大家分享一篇利用python和ffmpeg 批量將其他圖片轉(zhuǎn)換為.yuv格式的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01Python中dilb和face_recognition第三方包安裝失敗的解決
本文主要介紹了Python中dilb和face_recognition第三方包安裝失敗的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02tensorflow 只恢復(fù)部分模型參數(shù)的實例
今天小編就為大家分享一篇tensorflow 只恢復(fù)部分模型參數(shù)的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01pytorch中model.named_parameters()與model.parameters()解讀
這篇文章主要介紹了pytorch中model.named_parameters()與model.parameters()使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11