[問題] 如何同時讀寫一個檔案?

看板Python作者 (mymiss)時間11年前 (2014/09/12 19:08), 編輯推噓0(001)
留言1則, 1人參與, 最新討論串1/2 (看更多)
請問一下我在嘗試自動加資料到同一個檔案的實作 <?php $arr = array( array("A","B"), array("B","C"),) ?> array 大約有500多行 我想要讀取到空白行的時候塞值進去 最後的目的是要自動加資料到同一個檔案 不過我試著分開寫 也是得到同樣的結果(會有缺漏) 想請問有沒有什麼解決方法呢?? 以下是我的程式碼: searchline = '\n' inputfile = open('a.php','r') lines = inputfile.readlines() # f being the file handle i = lines.index(searchline) # Make sure searchline is actually in the file lines.insert(i+1, 'Different random text') outputfile = open('b.php','w') for line in lines: outputfile.write(line) -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.113.134.156 ※ 文章網址: http://www.ptt.cc/bbs/Python/M.1410520120.A.35A.html

09/12 21:39, , 1F
沒看到 ``oupputfile.close()`` , 這沒問題嗎?
09/12 21:39, 1F
文章代碼(AID): #1K4jGuDQ (Python)
文章代碼(AID): #1K4jGuDQ (Python)