• python拓展应用:运行do文件及其衍生内容


     1 #-*-coding:utf8-*-
     2 import os
     3 import subprocess
     4 import shutil
     5 import psutil
     6 import time
     7 
     8 
     9 #查找指定文件名称和查找范围的文件路径
    10 def search(name, path):
    11     name = name.lower()
    12     for root,dirs,files in os.walk(path):
    13         for file in files+dirs:
    14             if name in file.lower():
    15                 return os.path.join(root,file)
    16     return -1
    17 
    18 #修改do文件内容
    19 def do(in_file):
    20     w = ''
    21     for line in open('sort.do','r').readlines():
    22         if 'foreach' in line:
    23             ceil = line.split(" ")
    24             ceil[len(ceil)-1] = '"'+in_file+'"{
    '
    25             w = ""
    26             for i in ceil:
    27                 w = w + i + ' '
    28         else:
    29             w += line
    30 
    31     with open('sort.do','wb') as writer:
    32         writer.write(w)
    33 
    34 #运行指定的其他类型的程序,如do文件
    35 def run_do(do_file,in_file):
    36 
    37     do(in_file)
    38 
    39     # 查找windows所有盘符名称
    40     information = os.popen("wmic LOGICALDISK get name").read().split("
    ")
    41     code = []
    42     for inform in information:
    43         if ':' in inform:
    44             code.append(inform.strip())
    45     print code
    46 
    47     # 遍历所有盘符,直到找到StataMP-64.exe文件
    48     for path in code:
    49         stata_path = search('StataMP-64.exe', path + '\')
    50         if stata_path != -1:
    51             break
    52     print stata_path
    53 
    54     if os.path.exists('20140803'):
    55         pass
    56         print '已存在'
    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()
    63         while True:
    64             time.sleep(5)
    65             # 传入进程PID,实现监控功能
    66             cpu_percent = psutil.Process(child.pid).cpu_percent(interval=2)
    67             percent = cpu_percent/cpu_count
    68             print cpu_count,cpu_percent,percent
    69             if percent == 0:
    70                 child.kill()
    71                 print 'Success'
    72                 break
    73 
    74 if __name__ == '__main__':
    75 
    76     run_do('sort')
  • 相关阅读:
    【Android】Android连接SQLite3数据库的操作
    【exe4j】如何利用exe4j把java桌面程序生成exe文件
    Http网络协议
    【Spring】spring的7个模块
    【jsp】JSP中page指令isThreadSafe
    【MySQL】乐观锁和悲观锁
    【Eclipse】Eclipse上如何集成SVN插件
    【MySQL】mysql出现错误“ Every derived table must have its own alias”
    【Struts2】SSH如何返回JSON数据
    【Oracle】Oracle 的过程化SQL(PLSQL)中NULL值的处理
  • 原文地址:https://www.cnblogs.com/jpapplication/p/9789029.html
Copyright © 2020-2023  润新知