第一次執行此程序時,除了新行之外,生成的文件中沒有任何內容。但是第二次執行它時,它會正確寫入'out.txt',但第一次執行的新行仍然存在。爲什麼第一次不正常?寫入文件僅在第一次執行時給出新行
bhaarat = open('bhaarat.txt', 'r+')
bhaarat_read = bhaarat.read()
out = open('out.txt', 'r+')
out_read = out.read()
bhaarat_split = bhaarat_read.split()
for word in bhaarat_split:
if word.startswith('S') or word.startswith('H'):
out.write(word + "\n")
bhaarat.write('\n23. English\n')
print out_read
print bhaarat_read
bhaarat.close()
out.close()
您可以轉儲文件的內容以查看結構嗎? – Ketouem 2013-03-01 08:58:18
我很抱歉,我很新,所以我不太清楚你的意思。你的意思是'貓文件'來看看它的內容? – lche 2013-03-01 09:08:54