第0007題:有個目錄,里面是你自己寫過的程序,統計一下你寫過多少行代碼。包括空行和注釋,但是要分別列出來。
解題思路:
使用os模塊的listdir()
方法或walk()
方法得到目錄下自己的程序文件。然后讀取每個文件,分別統計代碼行、空行和注釋。
代碼如下:
#! /usr/bin/env python
#coding=utf-8
import os
def get_files(path):
files = os.listdir(path)
files_path = []
for fi in files:
fi_path = path + '\\' + fi
if os.path.isfile(fi_path):
if fi.split('.')[-1]=='py':
files_path.append(fi_path)
elif os.path.isdir(fi_path):
files_path += get_files(fi_path)
return files_path
def count(files):
line_of_code, blank, comments = 0, 0, 0
for filename in files:
f = open(filename, 'rb')
for line in f:
line = line.strip()
line_of_code += 1
if line == '':
blank += 1
elif line[0] == '#' or line[0] == '/':
comments += 1
f.close()
return (line_of_code, blank, comments)
if __name__ == '__main__':
files = get_files('.')
print files
lines = count(files)
print 'Line(s): %d, black line(s): %d, comments line(s): %d' % (lines[0], lines[1], lines[2])