题目

有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来。

代码

参照网络上代码

# coding: utf-8
import os
import re # 代码所在目录
FILE_PATH = 'TestDir' def count_line(file):
note_line = 0 # 注释行数
blank_line = 0 # 空行
with open(file, 'r', encoding='utf-8') as f:
lines = f.readlines()
total_line = len(lines) # 代码总行数
line_index = 0
# 遍历每一行
while line_index < total_line:
line = lines[line_index]
# 检查是否为注释
if line.startswith("#"):
note_line += 1
elif re.match(r"\s*'''", line) is not None:
note_line += 1
while re.match(".*'''$", line) is None:
line = lines[line_index]
note_line += 1
line_index += 1
# 检查是否为空行
elif line == "\n":
blank_line += 1
line_index += 1
print("====================================================")
print(" 在文件%s中:" % file)
print("----------------------------------------------------")
print("代码总行数:", total_line)
print("注释行数:", note_line, "占%0.2f%%" % (note_line * 100 / total_line))
print("空行数: ", blank_line, "占%0.2f%%" % (blank_line * 100 / total_line))
return [total_line, note_line, blank_line] def run(file_path):
# 切换到code所在目录
os.chdir(file_path)
# 遍历该目录下的py文件
total_lines = 0
total_note_lines = 0
total_blank_lines = 0
for i in os.listdir(os.getcwd()):
if os.path.splitext(i)[1] == '.py':
line = count_line(i)
total_lines = total_lines + line[0]
total_note_lines = total_note_lines + line[1]
total_blank_lines = total_blank_lines + line[2]
print("==================================================")
print("总代码行数:", total_lines)
print("总注释行数:", total_note_lines, "占%0.2f%%" % (total_note_lines * 100 / total_lines))
print("总空行数: ", total_blank_lines, "占%0.2f%%" % (total_blank_lines * 100 / total_lines)) if __name__ == '__main__':
run(FILE_PATH)

Python每日一题 009的更多相关文章

  1. Python每日一题 004

    将 0001 题生成的 200 个激活码(或者优惠券)保存到 Redis 非关系型数据库中. 代码 import redis import uuid # 创建实例 r=redis.Redis(&quo ...

  2. Python每日一题 003

    将 002 题生成的 200 个激活码(或者优惠券)保存到 MySQL 关系型数据库中. 代码 import pymysql import uuid def get_id(): for i in ra ...

  3. Python每日一题 002

    做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)? 在此生成由数字,字母组成的20位字 ...

  4. Python每日一题 008

    题目 基于多线程的网络爬虫项目,爬取该站点http://www.tvtv.hk 的电视剧收视率排行榜 分析 robots.txt User-agent: Yisouspider Disallow: / ...

  5. Python每日一题 007

    题目 你有一个目录,放了你一个月的日记,都是 txt,为了避免分词的问题,假设内容都是英文,请统计出你认为每篇日记最重要的词. 很难客观的说每篇日记中最重要的词是什么,所以在这里就仅仅是将每篇日记中出 ...

  6. Python每日一题 006

    题目 你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小. 如果只是单纯的通过将图片缩放到iPhone5分辨率大小,显然最后呈现出来的效果会很糟糕.所以等比例缩放到长( ...

  7. Python每日一题 005

    任一个英文的纯文本文件,统计其中的单词出现的个数. 代码 # coding:utf-8 import re def get_word(filename): fp=open(filename," ...

  8. Python每日一题 001

    Github地址:https://github.com/Yixiaohan/show-me-the-code Talk is Cheap, show me the code. --Linus Torv ...

  9. python每日一练:0007题

    第 0007 题: 有个目录,里面是你自己写过的程序,统计一下你写过多少行代码.包括空行和注释,但是要分别列出来. # -*- coding:utf-8 -*- import os def count ...

随机推荐

  1. java 通过反射调用属性,方法,构造器

    package reflection2; import static org.junit.Assert.assertArrayEquals; import java.lang.reflect.Cons ...

  2. C#读取csv、xls、sql数据库的实现

    using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...

  3. [CSP-S模拟测试]:Lighthouse(哈密顿回路+容斥)

    题目背景 $Billions\ of\ lighthouses...stuck\ at\ the\ far\ end\ of\ the\ sky.$ 题目描述 平面有$n$个灯塔,初始时两两之间可以相 ...

  4. [CSP-S模拟测试]:公园(BFS+剪枝)

    题目传送门(内部题31) 输入格式 第一行,五个整数$V,M,N,E,L$.接下来$M$行,每行两个正整数$s_i,a_i$.保证$s_i$互不相等.接下来$N$行,每行两个正整数$t_j,b_j$. ...

  5. PHP常用

    php.ini文件配置 时间配置PRC中国 date.timezone = PRC 文件上传file_uploads = On //支持http上传upload_tmp_dir = //临时文件保存路 ...

  6. Asp.Net Core 第03局:Startup

    总目录 前言 本文介绍Startup,它主要用于配置应用使用的服务和应用的请求管道. 环境 1.Visual Studio 2017 2.Asp.Net Core 2.2 开局 第一手:Startup ...

  7. 搜索的应用--计算最优解:Aizu - ALDS1_4_D Allocation

    搜索的应用-计算最优解 题目: You are given nn packages of wiwi kg from a belt conveyor in order (i=0,1,...n−1i=0, ...

  8. 正则split的使用

    import re #\s 空格 +号表示至少出现一次 # path = 'C:\\Users\\cz\Desktop\py06\\PY6_Day01\\爬虫作业\\2018_03_07\\05_sp ...

  9. 3.jmeter jsr232 脚本获取当前测试的正在活动的线程数

    jsr232 groovy 脚本获取当前测试的正在活动的线程数 (需要选择 groovy类型, 如果使用beanshell或者javascript,请根据其语法稍作修改即可) import org.a ...

  10. 再往DjVu鼓吹者的头上敲一棒子

    最近在某论坛又看到有人在鼓吹DjVu,甚至声称拿到PDG就转成DjVu,忍不住想再敲打敲打. 早几年前就已经有人举出过实例,证明PDG.TIFF转DjVu会因为有损压缩而产生错别字,似乎时间长了一堆新 ...