分享一个记录人员变化的脚本...

使用前提需要每天复制所有群成员的按钮,然后每天保存成文件放到脚本同目录就可以.

  1 #!/usr/bin/env python
  2 # -*- coding:utf-8 -*-
  3 import sys
  4 import os
  5 
  6 PRINTRED = "\033[1;31m"
  7 PRINTGREEN = "\033[0;32;47m"   #green color backgroud is white
  8 PRINTGREEN_SIM = "\033[1;32m"  #no backgroud green color
  9 PRINTBLUE = "\033[1;34m"
 10 PRINTCOLOR_END= "\033[0m"
 11 
 12 def getPersonList(fileName):
 13     try:
 14         fp = open(fileName, 'r')
 15         fLines = fp.readlines()
 16         fp.close()
 17         perList = []
 18         for line  in  fLines:
 19             perSplit = line.split(';')
 20             for item in perSplit:
 21               perList.append(item)
 22 
 23         #print(perList)
 24         return perList
 25     except IOError:
 26         print("error: file not found, please check it !!!")
 27         sys.exit(0)
 28 
 29 def calPersonDiffResult(orgList, compList):
 30     #find delete person
 31     countdel = 0
 32     countadd = 0
 33     for org in orgList:
 34         find = 0
 35         for comp in compList:
 36             if (org == comp):
 37                 find = 1
 38         if (find == 0):
 39             countdel += 1
 40             print(("减少人员姓名:[{cstart}%s{cend}] !").format(cstart=PRINTRED,cend=PRINTCOLOR_END) % org)
 41     print(("总减少人数:[{cstart}%d{cend}] 人!!!").format(cstart=PRINTRED,cend=PRINTCOLOR_END) % countdel)
 42 
 43     #find add person
 44     for comp in compList:
 45         find = 0
 46         for org in orgList:
 47             if (comp == org):
 48                 find = 1
 49         if (find == 0):
 50             countadd += 1
 51             print(("添加人员姓名:[{cstart}%s{cend}] !").format(cstart=PRINTGREEN_SIM,cend=PRINTCOLOR_END) % comp)
 52     print(("总添加人员人数:[{cstart}%d{cend}] 人!!!\n\n").format(cstart=PRINTGREEN_SIM,cend=PRINTCOLOR_END) % countadd)
 53 
 54 
 55 #默认选择当前路径人员列表文件
 56 def exploreFiles(curDir = './'):
 57     '''
 58     fList = []
 59     for root,dirs,files in os.walk(curDir):
 60         for file in files:
 61             fileSuffix = os.path.splitext(file)[1]
 62             if fileSuffix == '.py' or fileSuffix == '.pyc' or fileSuffix == '.swp':
 63                 print(("跳过非法文件 :[{cstart}%s{cend}]").format(cstart=PRINTBLUE,cend=PRINTCOLOR_END) % file)
 64             else:
 65                 #print('append %s' % (file))
 66                 fList.append(file)
 67     return fList
 68     '''
 69 
 70     #按修改时间获取排序文件
 71     dir_list = os.listdir(curDir)
 72     if dir_list == None:
 73         print(("{cstart}路径未找到{cend}").format(cstart=PRINTRED,cend=PRINTCOLOR_END))
 74         return
 75     else:
 76         dir_list = sorted(dir_list,key=lambda x: os.path.getmtime(os.path.join(curDir, x)))
 77         # print(dir_list)
 78 
 79     #过滤不需要解析的非法格式文件
 80     fList = []
 81     for file in dir_list:
 82         fileSuffix = os.path.splitext(file)[1]
 83         if fileSuffix == '.py' or fileSuffix == '.pyc' or fileSuffix == '.swp':
 84             print(("跳过非法文件 :[{cstart}%s{cend}]").format(cstart=PRINTBLUE,cend=PRINTCOLOR_END) % file)
 85         else:
 86             #print('append %s' % (file))
 87             fList.append(file)
 88 
 89     return fList
 90 
 91 def main():
 92 
 93     fileList = exploreFiles()
 94     fileNum = len(fileList)
 95 
 96     for i in range(fileNum-1):
 97         for j in range(i+1, fileNum):
 98             fNameOrg= fileList[i]
 99             fNameComp = fileList[j]
100             orgPersionList = getPersonList(fNameOrg)
101             compPersionList =  getPersonList(fNameComp)
102             #delete ' ' person
103             countOrg = len(orgPersionList) - 1
104             countComp =  len(compPersionList) - 1
105             print(("%s:原始人数:[{cstart}%d{cend}] 人!!!").format(cstart=PRINTRED,cend=PRINTCOLOR_END) % (fNameOrg, countOrg))
106             print(("%s:现有人数:[{cstart}%d{cend}] 人!!!").format(cstart=PRINTGREEN_SIM,cend=PRINTCOLOR_END) % (fNameComp, countComp))
107             calPersonDiffResult(orgPersionList, compPersionList)
108 
109 
110 if __name__=="__main__":
111   main()

 

posted @ 2020-07-30 11:12  代码的搬运工  阅读(547)  评论(0编辑  收藏  举报