zoukankan      html  css  js  c++  java
  • python--代码统计(进阶版)

      在上一篇的随笔中发表了代码统计小程序,但是发表后,我发现,以前写的代码怎么办

      写了那么多,怎么就从0开始了呢,,,,我还是个孩子啊,不能这么残忍

      于是,代码统计进阶版:统计当前目录下所有指定文件类型的行数

      

     1 #coding:gbk
     2 import os
     3 import time
     4 from CountItem.FindCode import *
     5 
     6 n = 0
     7 '''查找历史记录的行数'''
     8 try:
     9     with open('TotalLines','r') as p:
    10         lastline = ''
    11         for lastline in p.readlines():
    12             pass
    13         index = lastline.find('>>')
    14         n = int(lastline[index+2:])
    15 except ValueError as e:
    16     n = 0
    17 except FileNotFoundError:
    18     n = 0
    19 '''文件列表'''
    20 fileList = FindCode().getLocalfile(os.getcwd())
    21 '''计算行数'''
    22 for filename in fileList:
    23     try:
    24         with open(filename,'r') as f:
    25             try:
    26                 lines = f.readlines()
    27             except UnicodeDecodeError:
    28                 '''编码错误,不用管,我们要的是行数'''
    29                 '''嗯,,掩耳盗铃'''
    30                 pass
    31             for s in lines:
    32                 '''不计入空行'''
    33                 if s == '
    ':
    34                     continue
    35                 n += 1
    36     except FileNotFoundError as e:
    37         print('文件名或文件路径错误,没有该文件!')
    38         os.system('pause')
    39         os._exit(1)
    40     except OSError as e:
    41         print('文件名不合法')
    42         os.system('pause')
    43         os._exit(1)
    44 
    45 print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) + '代码行数>>' + str(n) + '
    ')
    46 
    47 '''写入文件'''
    48 with open('TotalLines','a') as f:
    49     f.write(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) + '代码行数>>' + str(n) + '
    ')
    50 os.system('pause')
     1 #coding:gbk
     2 import os
     3 class FindCode():
     4     '''初始化查找的文件类型列表'''
     5     def __init__(self):
     6         self.targetFile = []
     7         self.targetFile.append('c')
     8         self.targetFile.append('cpp')
     9         self.targetFile.append('py')
    10         self.targetFile.append('java')
    11     '''递归查找文件,用绝对路径'''
    12     def getLocalfile(self,findpath):
    13         fileList = os.listdir(findpath) 
    14         aimsList = []
    15         for filepath in fileList:
    16             filepath = findpath + '\' + filepath
    17             if os.path.isdir(filepath):
    18                 aimsList.extend(self.getLocalfile(filepath))
    19             else:
    20                 if self.selectFile(filepath):
    21                     aimsList.append(filepath)
    22         return aimsList
    23     
    24     def selectFile(self,filepath):
    25         index = filepath.find('.')
    26         lastname = filepath[index+1:]
    27         return True if lastname in self.targetFile else False
    28     
    29 if __name__ == '__main__':
    30     Demo = FindCode()
    31     print('
    '.join(x for x in Demo.getLocalfile(os.getcwd())))
    32     

    但是,不能统计一次挪一次窝吧,再改!

    1 '''文件列表'''
    2 filepath = input('请输入指定文件路径(绝对路径):')
    3 if os.path.isdir(filepath):
    4      fileList = FindCode().getLocalfile(filepath)

    这个就对了,很爽,,,

  • 相关阅读:
    背水一战 Windows 10 (90)
    背水一战 Windows 10 (89)
    背水一战 Windows 10 (88)
    背水一战 Windows 10 (87)
    背水一战 Windows 10 (86)
    背水一战 Windows 10 (85)
    背水一战 Windows 10 (84)
    背水一战 Windows 10 (83)
    背水一战 Windows 10 (82)
    背水一战 Windows 10 (81)
  • 原文地址:https://www.cnblogs.com/slothrbk/p/7253589.html
Copyright © 2011-2022 走看看