關於Python 腳本如何執行另一個腳本,可以使用os.system()來實現
os.system()的參數: 執行的命令 +執行的內容
舉例說明:
(1)顯示當前文件夾下的全部目錄和文件夾
os.system('dir') //dir 顯示磁盤目錄命令
(2)刪除指定文件夾下的文件
os.system('del e:\\test\\test.txt') //del 刪除指定文件 + 要刪除文件名
(3)刪除一個空文件夾
os.system('rd e:\\test') //rd(RMDIR):在DOS操作系統中用於刪除一個目錄 + 要刪除文件夾
(4)關閉進程
os.system('taskkill /F /IM chrome.exe') //taskkill是用來終止進程的 + 進程名
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
本實驗中用A.py 執行B.py,其中B.py執行時需要添加參數(cmd下執行命令:
>Python B.py IC.txt)
A.py 實驗代碼如下:
import os
str=('python B.py IC.txt') //python命令 + B.py + 參數:IC.txt'
p=os.system(str)
print(p) //打印執行結果 0表示 success , 1表示 fail
1
2
3
4
如果B.py執行時不需要添加參數(cmd下執行命令:>Python B.py)
import os
str=('python B.py') //python命令 + B.py
p=os.system(str)
print(p) //打印執行結果 0表示 success , 1表示 fail
---------------------
原文:https://blog.csdn.net/shenjin_s/article/details/79976954