0
我有一個文件,我想從它複製只有特定的行到另一個文件。我試圖創建一個功能:python:從一個文件複製特定的行到另一個文件
def copytotemp(logfile, fromline, toline):
with open(logfile) as origfile:
with open("templog.log", "w") as tempfile:
for num, line in enumerate(origfile, 0):
if (num + 1) <= fromline and (num + 1) >= toline:
tempfile.write(line)
但tempfile.log總是空空的。 謝謝
'如果(NUM + 1)<= fromline和(NUM + 1)> = toline:'?你確定 ?看起來你有比較運算符向後... –
哦,是的順便說一聲:'enumerate()'採用可選的'start'參數(默認爲'0'),所以如果你想要基於1的行號,你可以使用'enumerate (origfile,1)'並擺脫'num + 1'的東西。 –
謝謝,這是問題:) –