python拓展應用 執行do檔案及其衍生內容

2021-09-26 01:58:13 字數 2524 閱讀 7036

1

#-*-coding:utf8-*-

2importos3

import

subprocess

4import

shutil

5import

psutil

6import

time78

9#查詢指定檔名稱和查詢範圍的檔案路徑

10def

search(name, path):

11 name =name.lower()

12for root,dirs,files in

os.walk(path):

13for file in files+dirs:

14if name in

file.lower():

15return

os.path.join(root,file)

16return -1

1718

#修改do檔案內容

19def

do(in_file):

20 w = ''

21for line in open('

sort.do

','r

').readlines():

22if

'foreach'in

line:

23 ceil = line.split("")

24 ceil[len(ceil)-1] = '

"'+in_file+'

"{\n

'25 w = ""

26for i in

ceil:

27 w = w + i + ''28

else

:29 w +=line

3031 with open('

sort.do

','wb

') as writer:

32writer.write(w)

3334

#執行指定的其他型別的程式,如do檔案

35def

run_do(do_file,in_file):

3637

do(in_file)

3839

#查詢windows所有碟符名稱

40 information = os.popen("

wmic logicaldisk get name

").read().split("

\r\n")

41 code =

42for inform in

information:

43if':

'ininform:

4445

print

code

4647

#遍歷所有碟符,直到找到statamp-64.exe檔案

48for path in

code:

49 stata_path = search('

statamp-64.exe

', path + '\\'

)50if stata_path != -1:

51break

52print

stata_path

5354

if os.path.exists('

20140803'):

55pass

56print

'已存在'57

else

:58 do_file = os.path.abspath(do_file + '

.do'

)59 cmd = [stata_path,'do'

,do_file]

60 child =subprocess.popen(cmd)61#

找出本機邏輯核個數

62 cpu_count =psutil.cpu_count()

63while

true:

64 time.sleep(5)65#

傳入程序pid,實現監控功能

66 cpu_percent = psutil.process(child.pid).cpu_percent(interval=2)

67 percent = cpu_percent/cpu_count

68print

cpu_count,cpu_percent,percent

69if percent ==0:

70child.kill()

71print

'success'72

break

7374

if__name__ == '

__main__':

7576 run_do('

sort

')

Xshell連線伺服器執行python檔案

第一步,在xshell中輸入ssh stu 115.230.221.100 回車 彈出這樣乙個視窗。第二步,輸入密碼,連線伺服器。下附 連線伺服器成功的 第三步,輸入cd pimodel ts cd 你的目錄 進入到你要執行的資料夾下。第四步,輸入ls,檢視當前目錄下的檔案 第五步,輸入python...

如何在Linux上直接執行python檔案

在linux系統中,如何直接執行編寫好的python程式?而不用每次寫,python3 檔名 首先確定一下python3的系統路徑 1 which python3 ubuntu18.04 顯示的結果是 usr bin python3 結果將用在python原始檔中 2 編輯python原始檔 以te...

Python執行某一資料夾下的所有py檔案

記一段 是使用python執行某一資料夾下的所有py檔案 coding utf 8 import os 當前指令碼所在的檔案絕對路徑 cur path os.path.dirname os.path.realpath file 將當前路徑設定為python的臨時環境變數,用於命令執行,需要設定是因為...