Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何避免覆盖文本文件?_Python_File_Overwrite - Fatal编程技术网

Python 如何避免覆盖文本文件?

Python 如何避免覆盖文本文件?,python,file,overwrite,Python,File,Overwrite,我在Python中使用PyGame的帮助下使用了一个单击程序。我想通过打开程序、关闭程序并再次打开来多次使用该程序 基本上,我打开程序,点击一些东西,它会把点击时间写入一个.txt文件。我的问题是,当我关闭程序并再次运行它时,它会覆盖.txt文件。所以我的问题是,如何避免这种情况 #Pygame program.... f = open("test.txt","w") f.write("write something") 以附加模式打开文件 f = open('test.txt', 'a')

我在Python中使用PyGame的帮助下使用了一个单击程序。我想通过打开程序、关闭程序并再次打开来多次使用该程序

基本上,我打开程序,点击一些东西,它会把点击时间写入一个.txt文件。我的问题是,当我关闭程序并再次运行它时,它会覆盖.txt文件。所以我的问题是,如何避免这种情况

#Pygame program....
f = open("test.txt","w") 
f.write("write something")

以附加模式打开文件

f = open('test.txt', 'a')

使用文件模式
'a'
(append)而不是
'w'
。chang
f=open(“test.txt”,“a”)
,这意味着append modeohh,那容易吗?“a”是什么意思?它的意思是附加