簡(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刪除列表元素del,pop(),remove()及clear()
這篇文章主要介紹了python刪除列表元素del,pop(),remove()及clear(),列表元素能增加就可以刪除,這里要給大家介紹的是刪除列表元素,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-03-03
PIP和conda 更換國(guó)內(nèi)安裝源的方法步驟
這篇文章主要介紹了PIP和conda 更換國(guó)內(nèi)安裝源的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Python利用pywin32實(shí)現(xiàn)自動(dòng)操作電腦
在windows系統(tǒng)上,重復(fù)性的操作可以用Python腳本來(lái)完成,其中常用的模塊是win32gui、win32con、win32api,要使用這三個(gè)模塊需要先安裝pywin32。本文就為大家介紹了如何利用這些模塊實(shí)現(xiàn)自動(dòng)操作電腦,感興趣的可以了解一下2022-11-11
對(duì)python中大文件的導(dǎo)入與導(dǎo)出方法詳解
今天小編就為大家分享一篇對(duì)python中大文件的導(dǎo)入與導(dǎo)出方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
怎么用Python識(shí)別手勢(shì)數(shù)字
今天給大家?guī)?lái)的文章是怎么用Python識(shí)別手勢(shì)數(shù)字,文中有非常詳細(xì)的圖文示例,對(duì)正在學(xué)習(xí)python的小伙伴們很有幫助,需要的朋友可以參考下2021-06-06

