记录下脚本内容,后面待完善,不一定是通用的。
# -*- coding: utf-8 -*- import _winreg import sys import os def closeReg(key): _winreg.CloseKey(key) def openReg(key): try: return _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,key) except: pass def queryVal(key,name): try: (value, type) = _winreg.QueryValueEx(key,name) closeReg(key) return value except: pass def getDirVersion(path): #列出当前path目录下的所有目录 dirarr = os.listdir(path) for dir in dirarr: #如果目录名中包含fluent字符串 if "fluent" in dir: #得到删掉fluent字符串后的内容,即版本字符串 return dir.split('fluent')[1] break return "" def getFluentDir_REG(): #定义所有可能的版本 fluentVer = ["v100", "v110", "v120", "v130", "v140", "v150", "v160", "v170", "v180", "v190", "v200"] for ver in fluentVer: keypath = "SOFTWARE\Classes\ANSYS " + ver + " .cas File\Shell\open\command" key = openReg(keypath) val = queryVal(key, "") if (str(val) != "None"): exepath = val.split('"')[1] #获取exe文件路径的目录名 dirname = os.path.dirname(exepath) #获取指定目录的上上个目录 topdir = os.path.dirname(os.path.dirname(dirname)) return topdir return "" def getFluentDir_ENV(): #获取环境变量值FLUENT_INC value = os.environ.get("FLUENT_INC") if str(value) != "None": return str(value) else: return "" def main(): path = getFluentDir_ENV() if path == "": path = getFluentDir_REG() if str(path) != "" and str(path) != "None": version = getDirVersion(str(path)) print version if __name__=="__main__": main()