defjava_c_lines_count(file_path,encoding):#统计java或者C代码行数lines_count_java_c = 0flag_java = 0try:withopen(file_path, "r", encoding=encoding) asfp:forline infp:ifline.strip[: 2] == "/*"orline.strip[ -2:] == "*/": # java和 C语言的多行注释flag_java += 1elifflag_java % 2!= 0:continueelifline.strip instring.whitespace: # 空行passelifline.lstrip[: 2] == "//": ##java和 C语言的单行注释passelse:lines_count_java_c += 1returnlines_count_java_cexceptException ase:print(e)return0
3.实现函数,从队列中获取文件路径,并统计代码行
1)使用queue.empty判断,当队列不为空时,从队列中获取文件路径
2)使用os.path.splitext,根据文件后缀判断,调用对应统计代码行函数,计算每个文件的代码行数
3)将每个文件的代码行数,分别加到对应的进程共享变量total_count_python,total_count_java,total_count_c的value值中。
defget_code_lines(queue,total_count_python,total_count_java,total_count_c):# 从队列中获取文件,并统计代码行数whilenotq