2013-08-04 38 views
0

命令我是新來的Python,我有以下問題:.txt文件與連結線被寫入後不打印在python

我寫的第一個72線.txt的文件來另一個.txt文件,textA.txt。

textA = open('textA.txt', 'w') 
textA.write('\n'.join(lines[1:72])) 
textA.close 

現在,按照我的意思,textA文件包含72個句子,每個句子從一個新行開始。 然而,當我做一個行數或我想通過

f=open ('textA.txt','r') 
print f.read() 

沒有打印的文件發生(與非空行數爲零)。

有人可以幫我嗎?

+0

什麼是'lines'在你的第一個片段? –

+3

'textA.close()',你錯過了括號。 –

回答

2

看起來好像您還沒有關閉文件句柄,並且write可能尚未完成。需要調用close函數:: textA.close()

無需擔心記住關閉文件,請使用with聲明。

with open('textA.txt', 'w') as f: 
    f.write('\n'.join(lines[1:72])) 

,然後讀迴文件,按要求

with open('textA.txt') as f: 
    print f.readlines()