给定一个文件,将其复制到/tmp目录下
sourceFile = '/var/log/messages'
targetFile = '/tmp/copy_messages'
with open(sourceFile, 'r') as oldFile:
result = oldFile.readlines()
with open(targetFile, 'w') as newFile:
for lines in result:
newFile.write(lines)
给定一个文件,统计其中关键字出现的次数并且排序
fileName = "/opt/Workspace/redis.conf"
wordDict = {}
removeList = ['', 'a', 'I',
'be', 'is', 'of', 'in', 'on', 'or', 'to', 'if', 'it', 'at', 'me', 'by','as',
'the', 'The', 'and', 'are', 'for', 'not', 'but', 'can', 'may', 'you',
'this', 'that', 'will', 'with', 'when'
]
with open(fileName, 'r', encoding='utf-8') as myFile:
for lines in myFile:
words = lines.split()
for word in words:
word = word.strip(' .#')
wordDict[word] = wordDict.get(word, 0) + 1
for item in removeList:
wordDict.pop(item, None)
result = sorted(wordDict.items(), key=lambda i: i[1], reverse=True)[0:10]
print(result)
文档更新时间: 2020-04-20 00:09 作者:闻骏