您的位置:首页 > 移动开发 > 微信开发

python小程序查看自己写了多少行代码

2017-08-05 00:09 225 查看


近两个月白天都在加班,晚上在恶补深度学习的知识以及学习tensorflow,后面学着学着感觉paddlepaddle框架(百度继Echarts外又一良心巨作!)也是不错的选择,有很详细的说明文档,对深度学习神经网络感兴趣的同学可以作为入门的选择,个人还未尝试,后期有时间可能会针对paddlepaddle进行学习并写些心得。

好了言归正传,5月低的时候临近毕业,突发奇想想知道知道大二开始捡起python到现在一共写了多少行代码,也算是给自己一个评定吧。

原理很简单,利用文件夹遍历方式获取.py文件,读取py文件内的行数。

# -*- coding: utf-8 -*-
# @Date    : 2017-05-25 23:46:39
# @Author  : Alan Lau (rlalan@outlook.com)
# @Version : Python3.5

import os

def fw(path):
fileArray = []
for root, dirs, files in os.walk(path):
for fn in files:
if (fn.split('.'))[-1] == 'py' and r'D:\Sofrware\Python35' not in root:
# 判断是否为py文件,因为我的python第三方包安装在了D:\Sofrware\Python35这个路径下,因此跳过这个文件夹
file = str(root+'\\'+fn)
fileArray.append(file)
return fileArray

def ifcode(path):
files = fw(path)
return files

def line_counter(file):
counter = 0
try:
with open(file, 'rb') as f:
content = f.readlines()
content = list(filter(lambda line: line != b'\r\n', content))  # 过滤空行
counter = len(content)
except Exception as e:
counter = 0
pass
else:
pass
finally:
pass
return counter

from datetime import datetime

def main():
path = r'D:\\'
counter = 0
files = ifcode(path)
i = 0
for file in files:
i += 1
counter += line_counter(file)
if counter > 10*i:
print('got %s lines.' % counter)
print('u have done %d python files.' % len(files))
print('u have done %d lines code, good luck Alan...' % counter)

if __name__ == '__main__':
t_s = datetime.now()
main()
t_e = datetime.now()
print('[finished in %s]' % (t_e-t_s))


结果:



4487112行代码……
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐