Python每日一题 009
题目
有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来。
代码
参照网络上代码
# 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的更多相关文章
- Python每日一题 004
将 0001 题生成的 200 个激活码(或者优惠券)保存到 Redis 非关系型数据库中. 代码 import redis import uuid # 创建实例 r=redis.Redis(&quo ...
- Python每日一题 003
将 002 题生成的 200 个激活码(或者优惠券)保存到 MySQL 关系型数据库中. 代码 import pymysql import uuid def get_id(): for i in ra ...
- Python每日一题 002
做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)? 在此生成由数字,字母组成的20位字 ...
- Python每日一题 008
题目 基于多线程的网络爬虫项目,爬取该站点http://www.tvtv.hk 的电视剧收视率排行榜 分析 robots.txt User-agent: Yisouspider Disallow: / ...
- Python每日一题 007
题目 你有一个目录,放了你一个月的日记,都是 txt,为了避免分词的问题,假设内容都是英文,请统计出你认为每篇日记最重要的词. 很难客观的说每篇日记中最重要的词是什么,所以在这里就仅仅是将每篇日记中出 ...
- Python每日一题 006
题目 你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小. 如果只是单纯的通过将图片缩放到iPhone5分辨率大小,显然最后呈现出来的效果会很糟糕.所以等比例缩放到长( ...
- Python每日一题 005
任一个英文的纯文本文件,统计其中的单词出现的个数. 代码 # coding:utf-8 import re def get_word(filename): fp=open(filename," ...
- Python每日一题 001
Github地址:https://github.com/Yixiaohan/show-me-the-code Talk is Cheap, show me the code. --Linus Torv ...
- python每日一练:0007题
第 0007 题: 有个目录,里面是你自己写过的程序,统计一下你写过多少行代码.包括空行和注释,但是要分别列出来. # -*- coding:utf-8 -*- import os def count ...
随机推荐
- Tortoise git账号记住密码
方法一: 本地git仓库目录下,找到 .git/config ,然后打开增加: [credential] helper = store 这样只有该目录下的git操作不用每次输入用户名和密码,换一个目录 ...
- 31 July
P1005 矩阵取数游戏 高精度不能更坑-- #include <cstdio> #include <cstring> struct INT { long long h, l; ...
- 认识setFactory
平常设置或者获取一个View时,用的较多的是setContentView或LayoutInflater#inflate,setContentView内部也是通过调用LayoutInflater#inf ...
- python生成接口自动化测试报告模版
1:准备html模版 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> &l ...
- 2018-2019-2 网络对抗技术 20165206 Exp 8 Web基础
- 2018-2019-2 网络对抗技术 20165206 Exp 8 Web基础 - 实验任务 (1).Web前端HTML(0.5分) 能正常安装.启停Apache.理解HTML,理解表单,理解GE ...
- 用 Flask 来写个轻博客 (24) — 使用 Flask-Login 来保护应用安全
Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 扩展阅读 用户登录帐号 用户登录状态 Flask-Login 使用 F ...
- Android深度探索-卷1第六章心得体会
这章主要介绍了第一个linux驱动程序:统计单词个数.Linux系统将每一个驱动都映射成一个文件,这些文件称为设备文件或驱动文件,都保存在/dev目录中.大多数Linux驱动都有与其对应的设备文件,因 ...
- 利用单臂路由实现VLAN间路由(有1个疑问)
配置PC机: PC1:IP 192.168.1.1 :掩码:255.255.255.0:网关:192.168.1.254 VLAN 10 PC2:IP 192.168.2.1 :掩码:255.255 ...
- pytorch与numpy中的通道交换问题
pytorch网络输入图像的格式为(C, H, W),而numpy中的图像的shape为(H,W,C) 所以一般需要变换通道,将numpy中的shape变换为torch中的shape. 方法如下: # ...
- 【设计模式】FactoryPattern工厂模式
Factory Pattern 简单工厂模式 将变化的部分封装起来 //简单工厂 class SimpleProductFactory{ Product createProduct(String ty ...