看板 Python 關於我們 聯絡資訊
你好,請問不知道為什麼,我底下的for loop只會跑一次: import os output_file1=open('result_exception.txt','w',encoding="utf-8") all_error_output=open('all_error_output.txt',encoding="utf-8") exceptionlist=open('exceptionlist.txt',encoding="utf-8") for each_line1 in all_error_output: print(each_line1,end="",file=output_file1) for each_line2 in exceptionlist: print(each_line2,end="",file=output_file1) output_file1.close() exceptionlist.close() all_error_output.close() 底下是all_error_output.txt內容 1;aaaa;1111 2;bbbb;2222 3;cccc;3333 底下是all_error_output.txt內容 1;aaaa;111 2;bbbb;222 底下是結果 1;aaaa;1111 1;aaaa;111 2;bbbb;222 2;bbbb;2222 3;cccc;3333 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 182.234.234.100
jlhg:你想跑什麼結果? 11/02 20:52
grtfor:因為檔案就像tape一樣,不重新開關檔,它會讀上一次的位置 11/02 20:59
grtfor:或者你可以用seek(0) 11/02 21:02
hyderai:因為第二層的for跑完一輪,位置就是指向檔案最後啦 11/02 21:06
jlhc:看到一樓想說 我怎麼自己推文了 11/03 01:29
yycsan:謝謝大家....seek(0)解決了我低問題...謝謝.. 11/07 00:16