代码
import glob
import os
def get_files():
file_full = ''.join([os.getcwd(), '/code/*.py'])
files = glob.glob(file_full)
return files
def count_lines(files):
for file in files:
total_lines = 0
notes_lines = 0
blank_lines = 0
print("Filename is {0}".format(file.split(r'/')[-1]))
with open(file) as lines:
for line in lines:
total_lines += 1
if line == '\n':
blank_lines += 1
elif line.startswith('#'):
notes_lines += 1
print_result(total_lines, notes_lines, blank_lines)
def print_result(total_lines,
notes_lines,
blank_lines):
print("Total is {0}, Notes is {1}, Blank is {2}\n".format(total_lines,
notes_lines,
blank_lines))
if __name__ == '__main__':
files = get_files()
count_lines(files)
新知识
按行读入
lines = open('/home/1.txt")
for line in lines:
print(line)