修改filename为文件夹or文件地址,然后统计所有python文件代码

import os
import sys
def count_code_lines(filename):
    res = os.walk(filename)
    count = 0
    for path, _, file_list in res:
        for file in file_list:
            filename = os.path.join(path, file)
            if filename.endswith('py'):
                with open(filename, 'r', encoding='utf8') as fr:
                    file_count = 0
                    for i in fr:
                        if i.startswith('#') or i.startswith('\n'):
                            continue
                        count += 1
                        file_count += 1
                    print(f'{filename}有{file_count}行')

    print(f'总共有{count}行')

if __name__ == '__main__':
    filename = sys.argv[1]
    # filename = '目录or文件地址'
    # count_code_lines(r'D:\上海python12期视频\python12期视频\项目-atm')
    count_code_lines(filename)

相关文章:

  • 2022-02-15
  • 2022-01-30
  • 2022-12-23
  • 2021-09-09
  • 2022-12-23
  • 2021-08-27
  • 2022-02-17
猜你喜欢
  • 2022-12-23
  • 2022-01-15
  • 2021-11-18
  • 2021-12-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案