重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Python实现代码统计工具-创新互联

本文实例为大家分享了Python实现代码统计工具的具体代码,供大家参考,具体内容如下

专注于为中小企业提供成都网站设计、成都网站制作服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业鹤山免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了1000多家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

思路:首先获取所有文件,然后统计每个文件中代码的行数,最后将行数相加.

实现的功能:

统计每个文件的行数;

统计总行数;

支持指定统计文件类型,排除不想统计的文件类型;

排除空行;

排除注释行

import os
import sys
import os.path
#for i in sys.argv:
# print (i)

# 判断单个文件的代码行数
def count_file_lines(file_path):
 line_count = 0
 flag=True
 try:
 fp = open(file_path,"r",encoding="utf-8")
 encoding_type="utf-8"
 fp.close()
 except:
 encoding_type="gbk"
 with open(file_path,"r",encoding=encoding_type) as fp:
 for line in fp:
 #print (line_count)
  if line.strip()=="":
  continue
  else:
  if line.strip().endswith("'''") and flag == False:
   flag=True
   continue
  if line.strip().endswith('"""') and flag == False:
   flag=True
   continue
  if flag == False:
   continue
  if line.strip().startswith("#encoding") or line.strip().startswith("#-*-"):
   line_count += 1
  #elif line.strip().startswith('"""') and line.strip().endswith('"""') and line.strip()!='"""':
 #continue
  #elif line.strip().startswith("'''") and line.strip().endswith("'''") and line.strip()!="'''":
 #continue
  elif line.strip().startswith('#'):
   continue
  elif line.strip().startswith("'''") and flag == True:
   flag = False
   continue
  elif line.strip().startswith('"""') and flag == True:
   flag = False
   continue
  else:
   line_count += 1
 return line_count

def count_code_lines(path,file_types=[]):
 # 判断路径是否存在
 if not os.path.exists(path):
 print("您输入的目录或文件路径不存在")
 return 0

 line_count=0 #代码行总数
 file_lines_dict={} #每个文件代码行数

 # 判断是否为文件
 if os.path.isfile(path):
 file_type = os.path.splitext(path)[1][1:] #取到文件后缀名

 # 判断文件类型是否满足条件
 if len(file_types)==0:
 file_types=["py","cpp","c","java","ruby","ini","go","html","css","js","txt","vbs","php","asp","sh"]
 if file_type in file_types:
 line_count = count_file_lines(path)
 return line_count
 else:
 file_path = []
 for root, dirs, files in os.walk(path):
  for file in files:
  file_path.append(os.path.join(root,file))
  for f in file_path:
   file_type = os.path.splitext(f)[1][1:]
   if len(file_types)==0:
   file_types=

["py","cpp","c","java","ruby","ini","go","html","css","js","txt","vbs","php","asp","sh"]
   if file_type not in file_types:
   continue
   line_num = count_file_lines(f)
   line_count += line_num
   file_lines_dict[f] = line_num
  return line_count,file_lines_dict
 

if __name__=="__main__":
 print (sys.argv)
 if len(sys.argv) < 2:
 print ("请输入待统计行数的代码绝对路径!")
 sys.exit()
 count_path = sys.argv[1]
 file_types = []
 if len(sys.argv) >2:
 for i in sys.argv[2:]:
  file_types.append(i)

#print(count_path,file_types)
print(count_code_lines(count_path,file_types))
#print(count_file_lines("b.py"))

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页名称:Python实现代码统计工具-创新互联
网页地址:http://cqcxhl.com/article/dipeid.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP