python--代码统计小程序

  有人说,大学生在校期间要码够10W行代码,也有人说,看的不是写代码的行数,而是修改代码的行数。。。

  不管谁说,说的人都挺牛的

  咳,首先给自己定个小目标吧,5W行代码!成天写代码,啥时候到5W呢?为了更准确的衡量自己,写了一个代码统计行数的小程序,初学编程的可以借鉴下,从开始统计自己的代码行数

  程序很简单,遍历代码文件的行数,将行数变量存放在本地文件中,可以在加个时间戳

 1 #coding=gbk
 2 import os
 3 import time
 4 from CountItem.FindCode import *
 5 
 6 n = 0
 7 '''查找历史记录的行数'''
 8 try:
 9     with open('TotalLines','r') as p:
10         lastline = ''
11         for lastline in p.readlines():
12             pass
13         index = lastline.find('>>')
14         n = int(lastline[index+2:])
15 except ValueError as e:
16     n = 0
17 except FileNotFoundError:
18     n = 0
19 '''文件列表'''
20 fileList = input('输入文件名,多个文件名以空格分隔:')
21 fileList = fileList = list(fileList.split(sep=' '))
22 '''计算行数'''
23 for filename in fileList:
24     try:
25         with open(filename,'r') as f:
26             try:
27                 lines = f.readlines()
28             except UnicodeDecodeError:
29                 '''编码错误,不用管,我们要的是行数'''
30                 '''嗯,,掩耳盗铃'''
31                 pass
32             for s in lines:
33                 '''不计入空行'''
34                 if s == '
':
35                     continue
36                 n += 1
37     except FileNotFoundError as e:
38         print('文件名或文件路径错误,没有该文件!')
39         os.system('pause')
40         os._exit(1)
41     except OSError as e:
42         print('文件名不合法')
43         os.system('pause')
44         os._exit(1)
45 
46 print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) + '代码行数>>' + str(n) + '
')
47 
48 '''写入文件'''
49 with open('TotalLines','a') as f:
50     f.write(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) + '代码行数>>' + str(n) + '
')
51 os.system('pause')

原文地址:https://www.cnblogs.com/langyao/p/7253433.html