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

作者: qoofamily (阿祐)   2014-09-14 00:13:53
searchline = ' \n'
inputfile = open('a.php','r')
lines = inputfile.readlines()
outputfile = open('b.php','w')
for line in lines:
if line==searchline :
outputfile.write('Different random text\n')
else :
outputfile.write(line)
※ 引述《mymiss312 (mymiss)》之銘言:
: 請問一下我在嘗試自動加資料到同一個檔案的實作
: <?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)

Links booklink

Contact Us: admin [ a t ] ucptt.com