2014-03-13 211 views
0

我需要将程序的输出添加到保存的文件中,每次运行程序时都不会覆盖以前的输出。Python:将字符串保存到文件而不覆盖文件的内容

这是我如何写一个文件现在:

lead.write(str(alldates)) 

任何想法?

+1

也许你应该花一些时间看看你可能使用的最基本的方法的文档,比如['open'](http://docs.python.org/2.7/library/functions.html#open )。 – metatoaster

+1

可能dup - http://stackoverflow.com/questions/4706499/how-do-you-append-to-file-in-python –

回答

0

你应该以追加模式打开文件。

1

在这里,你应该open文件中a PPEND模式:

append_text = str(alldates) 
with open('my_file.txt', 'a') as lead: 
    lead.write(append_text) 

有很多专业的方式来open文件用于不同的目的,并可以read about them in the documentation

相关问题