# 读取dumpPath.properties文件内容 with open(os.path.join(dumpPropertiesPath, 'dumpPath.properties'), 'r') as f: lines = f.readlines() # 记录匹配到的内容 matches = {} # 遍历每一行,进行模糊匹配 for line in lines: for s in distinctComponentsList: if line.startswith(s): matches[s] = line.split('=')[1].strip() break如何改造这段代码,除了匹配的,还要记录list中没有匹配上的值
时间: 2024-02-14 07:13:39 浏览: 98
可以在遍历`distinctComponentsList`时,使用`for-else`语句来判断是否匹配成功,如果没有匹配成功,则将该值记录到字典的默认值中。代码示例如下:
```python
with open(os.path.join(dumpPropertiesPath, 'dumpPath.properties'), 'r') as f:
lines = f.readlines()
matches = {}
not_matched = {}
for s in distinctComponentsList:
for line in lines:
if line.startswith(s):
matches[s] = line.split('=')[1].strip()
break
else:
not_matched[s] = None
```
这段代码会遍历`distinctComponentsList`中的每一个元素`s`,然后在`lines`中查找以`s`开头的行,如果找到了,则将匹配到的值存储在`matches`字典中,如果没有找到,则将`s`存储在`not_matched`字典中,值为`None`。最终,`matches`和`not_matched`中都存储了`distinctComponentsList`中的所有元素。
相关问题
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)
你可以将以上脚本封装成一个函数,如下所示:
```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 作为参数传递给它即可。
with open(os.path.join(path, filename), 'r') as f:
这是一个 Python 代码片段,用于打开指定路径下的文件并以只读模式读取文件内容。其中,path 是文件所在的路径,filename 是文件名。使用 with 语句可以确保文件在使用完后自动关闭,避免资源泄露。
阅读全文