.txt文件在python中使用join lines命令写入后不会打印
我是Python新手,我有以下问题: 我正在将.txt文件的前72行写入另一个.txt文件textA.txt.txt文件在python中使用join lines命令写入后不会打印,python,printing,line,text-files,Python,Printing,Line,Text Files,我是Python新手,我有以下问题: 我正在将.txt文件的前72行写入另一个.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() 什么也没有发生(非空行计数为零) 有人
textA = open('textA.txt', 'w')
textA.write('\n'.join(lines[1:72]))
textA.close
现在,正如我所想的,textA文件包含72个句子,每个句子都从一个新行开始。
但是,当我进行行计数或试图通过
f=open ('textA.txt','r')
print f.read()
什么也没有发生(非空行计数为零)
有人能帮我吗?看起来您还没有关闭文件句柄,而且
写入可能还没有完成<需要调用code>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()
第一个代码段中的
行是什么?textA.close()
,您错过了括号。