zoukankan      html  css  js  c++  java
  • 记录开发代码量的小程序

      这段时间在开发网站,想要去记录自己的开发代码量,所以写了一个小脚本,用来记录,代码如下:

     1 #-*- coding: UTF-8 -*-
     2 #用来检测当前的代码量
     3 import sys,os
     4 #全局变量,记录当前的代码行数
     5 count = 0
     6 #根据文件名称指定文件读取策略,可以选择读取哪些文件或者跳过某些文件,默认跳过所有文件,只读取指定文件.
     7 def fileCheckStrategy(file):
     8     if ".pyc" in file:
     9         return False
    10     if ".py" in file:
    11         return True
    12     return False
    13 #目录读取策略,同文件读取策略,用于跳过某些目录,默认目录都遍历
    14 def dirCheckStrategy(dir):
    15     return True
    16 
    17 #读取指定文件的代码行数
    18 def readfile(file):
    19     global count
    20     f = open(file,'r')
    21     read = f.readlines()
    22     for i in read:
    23         count+=1
    24     f.close()
    25 
    26 #遍历目录算法
    27 def check(currentpath):
    28     s = os.listdir(currentpath)
    29     os.chdir(currentpath)
    30     for x in s:
    31         os.chdir(currentpath)
    32         if os.path.isdir(x):
    33             if dirCheckStrategy(x):
    34                 #递归遍历指定目录
    35                 print "DIR   "+x
    36                 check(currentpath+"\"+x)
    37         elif os.path.isfile(x):
    38             if fileCheckStrategy(x):
    39                 #读取指定文件的代码行数
    40                 print "FILE  "+x
    41                 readfile(currentpath+"\"+x)
    42 
    43     pass
    44 
    45 if __name__ == '__main__':
    46     #a = sys.argv[0]
    47     #从当前工作目录开始,按照策略读取代码量
    48     path = os.getcwd()
    49     print path
    50     check(path)
    51     print count

    主要是用到一个递归来设计,算法较为简单  就不多写了

  • 相关阅读:
    Git -- 撤销修改
    Git -- 管理修改
    Git -- 相关命令
    Git -- 工作区 和 暂存区
    Git -- 基本操作 之 版本回退
    Git -- 创建版本库
    Git -- 安装
    Git -- 简介
    word文档下划线无法显示的解决方法
    The data protection operation was unsuccessful. This may have been caused by not having the user profile loaded for the current thread's user context,
  • 原文地址:https://www.cnblogs.com/color-my-life/p/3919869.html
Copyright © 2011-2022 走看看