Printing python 3.5没有打印输出到屏幕
我使用Python3.5创建了处理一个文件并将输出写入另一个文件的代码。以下是相关代码Printing python 3.5没有打印输出到屏幕,printing,python-3.5,Printing,Python 3.5,我使用Python3.5创建了处理一个文件并将输出写入另一个文件的代码。以下是相关代码 with open('2016_01_22_Investor_Companies_stops.txt','r') as stops_Investor_Companies: stops_Investor_Companies = stops_Investor_Companies.read() stops_Investor_Companies = nltk.word_tokenize(stops_I
with open('2016_01_22_Investor_Companies_stops.txt','r') as stops_Investor_Companies:
stops_Investor_Companies = stops_Investor_Companies.read()
stops_Investor_Companies = nltk.word_tokenize(stops_Investor_Companies)
stops_Investor_Companies= [w.lower() for w in stops_Investor_Companies]
stops_Investor_Companies = str(stops_Investor_Companies)
outfile = open ('stops_Investor_Companies_cln.txt', 'w')
outfile.write(stops_Investor_Companies)
print ('1. Investor Companies')
print (' ')
with open('stops_Investor_Companies_cln.txt','r') as fin:
print(fin.read())
print (' ')
结果是文本1。投资者公司
打印到屏幕上,但文件停止\u Investor\u Companies\u cln.txt
未打印到屏幕上
但是,我可以将文件stops\u Investor\u Companies\u cln.txt
打印到屏幕上,使用与单独脚本相同的代码段
with open('stops_Investor_Companies_cln.txt','r') as fin:
print(fin.read())
在再次打开文件之前,不能关闭该文件。只有在关闭文件后,才会在文件中刷新数据 在再次打开文件之前,请尝试关闭该文件:-
outfile = open ('stops_Investor_Companies_cln.txt', 'w')
outfile.write(stops_Investor_Companies)
outfile.close()
或者,您可以使用另一个with选项打开文件,以便关闭文件…由于您在再次打开文件之前尚未关闭文件,因此无法看到新数据。您应该做的是在打开文件进行写入时使用另一个with语句,以便正确关闭该文件