用python统计代码行的示例(包括空行和注释)

作者:ww23 时间:2022-06-28 02:15:30 

实例如下所示:


import os
import string

path = "/Users/U/workspace/python learning/show-me-the-code/0007/test/"
dir = os.listdir(path)

def count(file):
 total = 0 #总行数
 countPound = 0 #注释行数
 countBlank = 0 #空行数
 line = open(file,'r',encoding='utf-8') #打开文件,因为注释有中文所以使用utf-8编码打开
 for li in line.readlines(): #readlines()一次性读完整个文件
   total += 1
   if not li.split(): #判断是否为空行
     countBlank +=1
   li.strip()
   if li.startswith('#'):
     countPound += 1
 print(file)
 print("countBlank:%d" % countBlank)
 print("countPound:%d" % countPound)
 print("total:%d" % total)

for file in dir:
 count(path + file)

来源:https://blog.csdn.net/mihawk233/article/details/72796679

标签:python,统计,代码行
0
投稿

猜你喜欢

  • 页面制作中要注意的编码问题

    2008-08-11 12:43:00
  • asp如何动态生成WBMP?

    2009-11-18 20:36:00
  • python计数排序和基数排序算法实例

    2023-11-01 01:23:26
  • python实现简易的学生信息管理系统

    2021-08-24 12:07:09
  • PHP三层结构(上) 简单三层结构

    2023-06-20 06:36:07
  • driver = webdriver.Chrome()报错问题及解决

    2023-04-09 21:03:16
  • 人工智能学习Pytorch数据集分割及动量示例详解

    2021-04-29 11:28:55
  • python3 pillow模块实现简单验证码

    2021-07-04 11:10:44
  • 详解python多线程、锁、event事件机制的简单使用

    2022-03-16 19:48:25
  • 解决python 虚拟环境删除包无法加载的问题

    2023-09-15 14:43:57
  • 如何安装并使用conda指令管理python环境

    2022-07-02 04:08:43
  • 利用Opencv中Houghline方法实现直线检测

    2023-09-07 12:40:39
  • python 判断自定义对象类型

    2021-08-29 22:09:37
  • 用Python中的字典来处理索引统计的方法

    2022-05-28 19:43:45
  • GO语言的IO方法实例小结

    2023-09-17 08:47:24
  • Python使用Kubernetes API访问集群

    2023-09-23 05:35:31
  • 从IIS到SQL Server数据库安全

    2008-12-24 15:58:00
  • 使用python测试prometheus的实现

    2023-08-31 15:24:05
  • python使用cartopy库绘制台风路径代码

    2023-08-22 15:50:18
  • django自定义模板标签过程解析

    2023-07-29 12:34:57
  • asp之家 网络编程 m.aspxhome.com