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í)行過(guò)程中的輸出內(nèi)容
val = os.popen(cmd)
print(val)
print(val.read())
- 像調(diào)用”ls”這樣的shell命令揍魂,應(yīng)該使用popen的方法來(lái)獲得內(nèi)容