[英]Exclude empty lines and comment lines
import os
def countlines(start, lines=0, header=True, begin_start=None):
if header:
print('{:>10} |{:>10} | {:<20}'.format('ADDED', 'TOTAL', 'FILE'))
print('{:->11}|{:->11}|{:->20}'.format('', '', ''))
for thing in os.listdir(start):
thing = os.path.join(start, thing)
if os.path.isfile(thing):
if thing.endswith('.py'):
with open(thing, 'r') as f:
newlines = f.readlines()
newlines = list(filter(lambda l: l.replace(' ', '') not in ['\n', '\r\n'], newlines))
newlines = list(filter(lambda l: not l.startswith('#'), newlines))
newlines = len(newlines)
lines += newlines
if begin_start is not None:
reldir_of_thing = '.' + thing.replace(begin_start, '')
else:
reldir_of_thing = '.' + thing.replace(start, '')
print('{:>10} |{:>10} | {:<20}'.format(
newlines, lines, reldir_of_thing))
for thing in os.listdir(start):
thing = os.path.join(start, thing)
if os.path.isdir(thing):
lines = countlines(thing, lines, header=False, begin_start=start)
return lines
countlines(r'/Documents/Python/')
如果我們取標准的Python file.main.py,那么里面有4行代碼。 他算作5。如何解決? 如何正確設置過濾器,使其不計算空代碼行和注釋?
1.您可以修改您的第一個filter
條件: strip
該行,然后檢查它是否為空。
lambda l: l.replace(' ', '') not in ['\n', '\r\n']
變成
lambda l: l.strip()
2. filter
接受任何 iterable ,因此無需每次都將其轉換為列表 - 這是一種浪費,因為它強制進行兩組迭代 - 一組在您創建列表時,另一組在您第二次過濾時。 您可以刪除對list()
的調用,並且在完成所有過濾后只執行一次。 您還可以在文件句柄本身上使用filter
,因為文件句柄f
是一個可迭代的,它在每次迭代中從文件中產生行。 這樣,您只需遍歷整個文件一次。
newlines = filter(lambda l: l.strip(), f)
newlines = filter(lambda l: not l.strip().startswith('#'), newlines)
num_lines = len(list(newlines))
請注意,我重命名了最后一個變量,因為變量名應該描述它是什么
3. 您可以將兩個過濾條件組合成一個 lambda
lambda l: l.strip() and not l.strip().startswith('#')
或者,如果您有 Python 3.8+,
lambda l: (l1 := l.strip()) and not l1.startswith('#')
這使我的觀點 #2 關於不list
上述沒有意義的問題-
num_lines = len(list(filter(lambda l: (l1 := l.strip()) and l1.startswith('#'), f)))
使用以下輸入,這給出了正確的行數:
文件.py :
print("Hello World")
# This is a comment
# The next line is blank
print("Bye")
>>> with open('file.py') as f:
... num_lines = len(list(filter(lambda l: (l1 := l.strip()) and l1.startswith('#'), f)))
... print(num_lines)
Out: 2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.