我目前正在通過免費的在線python學校工作。下面的模板已經給了我,我將完成該函數,以便它返回文件的大小(字節數)和換行符的數量(「\ n」)。我完全被卡住了。任何幫助,將不勝感激。打開文件進行讀取並返回字節數和換行符的函數
def readFile(filename):
f = open(filename)
size = 0
lines = 0
buf = f.read()
while buf!="":
buf = f.read()
f.close()
return (size, lines)
呃。 Python'學校'不使用'with'。 xD – BeetDemGuise
這可能是一個非常早期的練習,他們不想混淆這個問題。 –