看板 Python 關於我們 聯絡資訊
※ 引述《opiumeater (opiumeater)》之銘言: : 今天才發現原來print可以這樣用: : f = open('txt','w') : for i in range(10): : print >> f,i 這個印完還是要 close 吧... with open('txt', 'w') as f: for i in range(10): print >> f,i : 我以前都會寫成 : f = open('txt','w') : a = map(str, range(10)) : txt = '\n'.join(a) : f.write(txt) : f.close() : 比較起來第一個好多了,跟大家分享。 -- "問おう、貴方が私のマスターか?" -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.32.81.146
opiumeater:沒錯,我忘了用with,感謝 03/24 23:08