How to avoid overwriting a text file? [duplicate] - python

This question already has answers here:
How do I append to a file?
(13 answers)
Closed 4 years ago.
I use a click program the help of PyGame in Python. I want to use the program multiple times by opening up the program, and shutting it down and reopening it again.
So basically I open the program, click something, and it writes the click time into a .txt file. My problem is, when I shut down the program and run it again, it overwrites the .txt file. So my question is, how to avoid this?
#Pygame program....
f = open("test.txt","w")
f.write("write something")

Open the file in append mode
f = open('test.txt', 'a')

Related

How to fix "io.UnsupportedOperation: File not open for writing" Error? [duplicate]

This question already has answers here:
How to open a file for both reading and writing?
(4 answers)
Closed 3 years ago.
In simplest terms, when i try to run a short amount of code to delete the contents of a file and then rewrite stuff to that file, it pulls that error. I'm trying to get a temperature reading from a com port using the filewrite from CoolTerm, perhaps it's the fact that the file is being used by CoolTerm as well, so I can't edit it, but I'm unsure.
I've tried multiple ways to delete the file information e.g the file.close(), and others, but none seem to work.
while True:
file = open("test.txt","r")
file.truncate()
x = file.read()
x = x.split("\n")
print(x[0])
print(x[1])
time.sleep(3)
I expect the console to output the contents of file but it doesn't. Something that gives me a similar result of what i want would be the Console just outputting the last two entries of the file, rather than having to delete all of it than rewriting it.
Modified to r+ mode is ok, I have tested.
with open('./install_cmd', 'r+') as f:
print(f'truncate ago:{f.read()}')
f.truncate(0)
print(f'truncate after:{f.read()}')

Cant write in a file to save a score on a game [duplicate]

This question already has answers here:
.write not working in Python
(5 answers)
Closed 3 years ago.
I am using:
text_file = open('files/highscoresnake.txt', 'w')
word = str(score)
text_file.write(word)
to save a highscore on a game.
however this does nothing to the text file
anyone know why?
You need to close the file when you're done. Try text_file.close() afterward.
Better yet, use with:
with open('files/highscoresnake.txt', 'w') as file:
file.write(word)
with will close the file after you exit that block of code.

How do I add text to a file without deleting what is already there? [duplicate]

This question already has answers here:
How do I append to a file?
(13 answers)
Closed 5 years ago.
I'm making a login system for my school task and when I try to add text to a Notepad file using Python it deletes what is already there so I can only store the information of one user at a time. Any idea how I can fix this? I've looked around on the internet but I can't find the right combination of words to get the search result I want... thanks in advance. I'm using IDLE (Python 3.2 GUI)
Edit: Problem solved, thanks for the help everyone.
You have to open file in append mode, for example:
f_obj = open('myfile','a+')
For that, you have to use "a" mode for appending data in a file.
f_obj = open("c:/file_path",'a')
f_obj.write("append")
or
with open("c:/file_path", "a") as f_obj:
f_obj.write("append")
You can use 'a for append' mode instead of 'r for right' mode, see following:
with open('your_exist_file.txt', 'a') as file:
file.write('appended_text')

python readline() output nothing [duplicate]

This question already has answers here:
Confused by python file mode "w+" [duplicate]
(11 answers)
Closed 6 years ago.
I got nothing back from the command readline(). I am new to python and totally confused now.
my_file = open("test.txt", "w+")
my_file.write("This is a test")
print my_file.readline()
When you write to a file, you overwrite any previous contents of the file and leave the pointer at the end of the file. Any attempt to read after that will fail, since you're already at the end of the file.
To reset to the beginning of the file and read what you just wrote, use:
my_file.seek(0)
Because after you wrote content in you file. the cursor is at the end of the file. Before you use readline(), use my_file.seek(0) first, If your file content is only This is a test, you can get your want. Deep into this, please go to : https://docs.python.org/2.7/tutorial/inputoutput.html#reading-and-writing-files

File gets cleared when try to write to it [duplicate]

This question already has answers here:
How do I append to a file?
(13 answers)
Closed 6 years ago.
I am try to make it possible to save progress in my python game put the following line is giving me problems:
dataW = open("data.dat","wb")
How can I stop this line from clearing my file.
You're opening the file in write mode:
'w' for only writing (an existing file with the same name will be
erased)
Emphasis mine
You meant to use the append mode:
'a' opens the file for appending

Categories

Resources