簡(jiǎn)單了解python調(diào)用其他腳本方法實(shí)例
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')
另外一個(gè)python腳本b.py如下:
#!/usr/local/bin/python3.7
print('hello world')
運(yùn)行結(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"
運(yùn)行結(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')
運(yùn)行結(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() 這個(gè)方法會(huì)打開(kāi)一個(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()方法。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python 二叉樹(shù)的層序建立與三種遍歷實(shí)現(xiàn)詳解
這篇文章主要介紹了Python 二叉樹(shù)的層序建立與三種遍歷實(shí)現(xiàn)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
pytorch 實(shí)現(xiàn)tensor與numpy數(shù)組轉(zhuǎn)換
今天小編就為大家分享一篇使用pytorch 實(shí)現(xiàn)tensor與numpy數(shù)組轉(zhuǎn)換,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
利用python和ffmpeg 批量將其他圖片轉(zhuǎn)換為.yuv格式的方法
今天小編就為大家分享一篇利用python和ffmpeg 批量將其他圖片轉(zhuǎn)換為.yuv格式的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
python生成隨機(jī)紅包的實(shí)例寫(xiě)法
在本篇文章里小編給大家整理的是關(guān)于python生成隨機(jī)紅包的實(shí)例寫(xiě)法以及相關(guān)知識(shí)點(diǎn),有需要的朋友們可以學(xué)習(xí)下。2019-09-09
tensorflow-gpu安裝的常見(jiàn)問(wèn)題及解決方案
這篇文章主要介紹了tensorflow-gpu安裝的常見(jiàn)問(wèn)題及解決方案,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧,需要的朋友可以參考下2020-01-01
Python中dilb和face_recognition第三方包安裝失敗的解決
本文主要介紹了Python中dilb和face_recognition第三方包安裝失敗的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
tensorflow 只恢復(fù)部分模型參數(shù)的實(shí)例
今天小編就為大家分享一篇tensorflow 只恢復(fù)部分模型參數(shù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01
pytorch中model.named_parameters()與model.parameters()解讀
這篇文章主要介紹了pytorch中model.named_parameters()與model.parameters()使用及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11

