Python調(diào)用python a.py腳本
# main.py
import time
import os
count = 0
str = ('python a.py')
result = os.system(str)
print(result)
while True:
count = count + 1
if count == 5:
print('this count is:',count)
break
else:
time.sleep(1)
print('this count is:',count)
# a.py
print('hello world')
> python main.py
hello world
this count is: 1
this count is: 2
this count is: 3
this count is: 4
this count is: 5
Python調(diào)用shell方法os.system()
# main.py
import time
import os
count = 0
# 返回值是腳本的退出狀態(tài)碼
n = os.system('sh a.sh')
while True:
count = count + 1
if count == 5:
print('this count is:',count)
break
else:
time.sleep(1)
print('this count is:',count)
# a.sh
echo "hello world"
> python main.py
hello world
this count is: 1
this count is: 2
this count is: 3
this count is: 4
this count is: 5
Python調(diào)用shell方法os.popen()
import os
if __name__ == "__main__":
print("this is a test file ")
cmd = "ls"
# 返回值是腳本執(zhí)行過程中的輸出內(nèi)容
val = os.popen(cmd)
print(val)
print(val.read())
- 像調(diào)用”ls”這樣的shell命令,應(yīng)該使用popen的方法來獲得內(nèi)容
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。