Python 将以下脚本定义成一个函数以供调用:# 利用set进行去重 distinctComponentsList = list(set(componentsList)) #print(distinctComponentsList) # 与/conf文件夹下的dumpPath.properties文件内容进行匹配 dumpPropertiesPath = os.path.abspath("../../conf") #print("dump properties路径为:" + dumpPropertiesPath) # 读取dumpPath.properties文件内容 with open(os.path.join(dumpPropertiesPath, 'dumpPath.properties'), 'r') as f: lines = f.readlines() #print(lines) # 记录匹配到的内容 matchedPathList = [] matches = {} # 遍历每一行,进行模糊匹配 for line in lines: for s in distinctComponentsList: if line.startswith(s): #matchedPathList.append(line.split('=')[1].strip()) matches[s] = line.split('=')[1].strip() break #print(matchedPathList) #print(matches) # 路径拼接 scanlist = [] for key in matches: for item in componentsFileList: if item.startswith(key): scanlist.append(item + matches[key]) needScanPathList = [allComponentPath + "/" + path for path in scanlist] #print(needScanPathList) # 使用 subprocess 调用 inotify.py 脚本,并将 needScanPathList 作为参数传递 pathParam = ' '.join(needScanPathList) #print(pathParam) order = ('nohup python inotify.py ' + pathParam + ' >> output.log 2>&1 &') sys.stdout = open('output.log', 'w') result = os.system(order)
时间: 2024-03-03 07:50:23 浏览: 160
你可以将以上脚本封装成一个函数,如下所示:
```python
import os
def start_inotify(componentsList, componentsFileList, allComponentPath):
distinctComponentsList = list(set(componentsList))
dumpPropertiesPath = os.path.abspath("../../conf")
with open(os.path.join(dumpPropertiesPath, 'dumpPath.properties'), 'r') as f:
lines = f.readlines()
matchedPathList = []
matches = {}
for line in lines:
for s in distinctComponentsList:
if line.startswith(s):
matches[s] = line.split('=')[1].strip()
break
scanlist = []
for key in matches:
for item in componentsFileList:
if item.startswith(key):
scanlist.append(item + matches[key])
needScanPathList = [allComponentPath + "/" + path for path in scanlist]
pathParam = ' '.join(needScanPathList)
order = ('nohup python inotify.py ' + pathParam + ' >> output.log 2>&1 &')
sys.stdout = open('output.log', 'w')
result = os.system(order)
```
你可以将以上代码复制到你的 Python 脚本中,然后调用 start_inotify 函数,并将 componentsList、componentsFileList 和 allComponentPath 作为参数传递给它即可。
阅读全文