美文网首页
0007-读出代码行书,注释以及空行

0007-读出代码行书,注释以及空行

作者: gogoforit | 来源:发表于2017-08-17 18:56 被阅读0次

代码

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)

相关文章

网友评论

      本文标题:0007-读出代码行书,注释以及空行

      本文链接:https://www.haomeiwen.com/subject/lcpnrxtx.html