我不明白为什么这行不通。我正在对传递给函数的字符串执行 lstrip(),并尝试查看它是否以“””开头。由于某种原因,它陷入了无限循环
def find_comment(infile, line):
line_t = line.lstrip()
if not line_t.startswith('"""') and not line_t.startswith('#'):
print (line, end = '')
return line
elif line.lstrip().startswith('"""'):
while True:
if line.rstrip().endswith('"""'):
line = infile.readline()
find_comment(infile, line)
else:
line = infile.readline()
else:
line = infile.readline()
find_comment(infile, line)
我的输出:
Enter the file name: test.txt
import re
def count_loc(infile):
这是我正在阅读以供参考的文件的顶部:
import re
def count_loc(infile):
""" Receives a file and then returns the amount
of actual lines of code by not counting commented
or blank lines """
loc = 0
func_records = {}
for line in infile:
(...)